UNPKG

812 BJavaScriptView Raw
1import { isLeapYear } from "./isLeapYear.js";
2import { toDate } from "./toDate.js";
3
4/**
5 * The {@link getDaysInYear} function options.
6 */
7
8/**
9 * @name getDaysInYear
10 * @category Year Helpers
11 * @summary Get the number of days in a year of the given date.
12 *
13 * @description
14 * Get the number of days in a year of the given date.
15 *
16 * @param date - The given date
17 * @param options - An object with options
18 *
19 * @returns The number of days in a year
20 *
21 * @example
22 * // How many days are in 2012?
23 * const result = getDaysInYear(new Date(2012, 0, 1))
24 * //=> 366
25 */
26export function getDaysInYear(date, options) {
27 const _date = toDate(date, options?.in);
28 if (Number.isNaN(+_date)) return NaN;
29 return isLeapYear(_date) ? 366 : 365;
30}
31
32// Fallback for modularized imports:
33export default getDaysInYear;