1 | import { constructFrom } from "./constructFrom.js";
|
2 | import { toDate } from "./toDate.js";
|
3 |
|
4 | /**
|
5 | * The {@link getDaysInMonth} function options.
|
6 | */
|
7 |
|
8 | /**
|
9 | * @name getDaysInMonth
|
10 | * @category Month Helpers
|
11 | * @summary Get the number of days in a month of the given date.
|
12 | *
|
13 | * @description
|
14 | * Get the number of days in a month of the given date, considering the context if provided.
|
15 | *
|
16 | * @param date - The given date
|
17 | * @param options - An object with options
|
18 | *
|
19 | * @returns The number of days in a month
|
20 | *
|
21 | * @example
|
22 | * // How many days are in February 2000?
|
23 | * const result = getDaysInMonth(new Date(2000, 1))
|
24 | * //=> 29
|
25 | */
|
26 | export function getDaysInMonth(date, options) {
|
27 | const _date = toDate(date, options?.in);
|
28 | const year = _date.getFullYear();
|
29 | const monthIndex = _date.getMonth();
|
30 | const lastDayOfMonth = constructFrom(_date, 0);
|
31 | lastDayOfMonth.setFullYear(year, monthIndex + 1, 0);
|
32 | lastDayOfMonth.setHours(0, 0, 0, 0);
|
33 | return lastDayOfMonth.getDate();
|
34 | }
|
35 |
|
36 | // Fallback for modularized imports:
|
37 | export default getDaysInMonth;
|