1 | import { differenceInCalendarWeeks } from "./differenceInCalendarWeeks.js";
|
2 | import { lastDayOfMonth } from "./lastDayOfMonth.js";
|
3 | import { startOfMonth } from "./startOfMonth.js";
|
4 | import { toDate } from "./toDate.js";
|
5 |
|
6 | /**
|
7 | * The {@link getWeeksInMonth} function options.
|
8 | */
|
9 |
|
10 | /**
|
11 | * @name getWeeksInMonth
|
12 | * @category Week Helpers
|
13 | * @summary Get the number of calendar weeks a month spans.
|
14 | *
|
15 | * @description
|
16 | * Get the number of calendar weeks the month in the given date spans.
|
17 | *
|
18 | * @param date - The given date
|
19 | * @param options - An object with options.
|
20 | *
|
21 | * @returns The number of calendar weeks
|
22 | *
|
23 | * @example
|
24 | * // How many calendar weeks does February 2015 span?
|
25 | * const result = getWeeksInMonth(new Date(2015, 1, 8))
|
26 | * //=> 4
|
27 | *
|
28 | * @example
|
29 | * // If the week starts on Monday,
|
30 | * // how many calendar weeks does July 2017 span?
|
31 | * const result = getWeeksInMonth(new Date(2017, 6, 5), { weekStartsOn: 1 })
|
32 | * //=> 6
|
33 | */
|
34 | export function getWeeksInMonth(date, options) {
|
35 | const contextDate = toDate(date, options?.in);
|
36 | return (
|
37 | differenceInCalendarWeeks(
|
38 | lastDayOfMonth(contextDate, options),
|
39 | startOfMonth(contextDate, options),
|
40 | options,
|
41 | ) + 1
|
42 | );
|
43 | }
|
44 |
|
45 | // Fallback for modularized imports:
|
46 | export default getWeeksInMonth;
|