1 | import { constructFrom } from "./constructFrom.mjs";
|
2 | import { startOfWeek } from "./startOfWeek.mjs";
|
3 | import { toDate } from "./toDate.mjs";
|
4 | import { getDefaultOptions } from "./_lib/defaultOptions.mjs";
|
5 |
|
6 | /**
|
7 | * The {@link getWeekYear} function options.
|
8 | */
|
9 |
|
10 | /**
|
11 | * @name getWeekYear
|
12 | * @category Week-Numbering Year Helpers
|
13 | * @summary Get the local week-numbering year of the given date.
|
14 | *
|
15 | * @description
|
16 | * Get the local week-numbering year of the given date.
|
17 | * The exact calculation depends on the values of
|
18 | * `options.weekStartsOn` (which is the index of the first day of the week)
|
19 | * and `options.firstWeekContainsDate` (which is the day of January, which is always in
|
20 | * the first week of the week-numbering year)
|
21 | *
|
22 | * Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
|
23 | *
|
24 | * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
|
25 | *
|
26 | * @param date - The given date
|
27 | * @param options - An object with options.
|
28 | *
|
29 | * @returns The local week-numbering year
|
30 | *
|
31 | * @example
|
32 | * // Which week numbering year is 26 December 2004 with the default settings?
|
33 | * const result = getWeekYear(new Date(2004, 11, 26))
|
34 | * //=> 2005
|
35 | *
|
36 | * @example
|
37 | * // Which week numbering year is 26 December 2004 if week starts on Saturday?
|
38 | * const result = getWeekYear(new Date(2004, 11, 26), { weekStartsOn: 6 })
|
39 | * //=> 2004
|
40 | *
|
41 | * @example
|
42 | * // Which week numbering year is 26 December 2004 if the first week contains 4 January?
|
43 | * const result = getWeekYear(new Date(2004, 11, 26), { firstWeekContainsDate: 4 })
|
44 | * //=> 2004
|
45 | */
|
46 | export function getWeekYear(date, options) {
|
47 | const _date = toDate(date);
|
48 | const year = _date.getFullYear();
|
49 |
|
50 | const defaultOptions = getDefaultOptions();
|
51 | const firstWeekContainsDate =
|
52 | options?.firstWeekContainsDate ??
|
53 | options?.locale?.options?.firstWeekContainsDate ??
|
54 | defaultOptions.firstWeekContainsDate ??
|
55 | defaultOptions.locale?.options?.firstWeekContainsDate ??
|
56 | 1;
|
57 |
|
58 | const firstWeekOfNextYear = constructFrom(date, 0);
|
59 | firstWeekOfNextYear.setFullYear(year + 1, 0, firstWeekContainsDate);
|
60 | firstWeekOfNextYear.setHours(0, 0, 0, 0);
|
61 | const startOfNextYear = startOfWeek(firstWeekOfNextYear, options);
|
62 |
|
63 | const firstWeekOfThisYear = constructFrom(date, 0);
|
64 | firstWeekOfThisYear.setFullYear(year, 0, firstWeekContainsDate);
|
65 | firstWeekOfThisYear.setHours(0, 0, 0, 0);
|
66 | const startOfThisYear = startOfWeek(firstWeekOfThisYear, options);
|
67 |
|
68 | if (_date.getTime() >= startOfNextYear.getTime()) {
|
69 | return year + 1;
|
70 | } else if (_date.getTime() >= startOfThisYear.getTime()) {
|
71 | return year;
|
72 | } else {
|
73 | return year - 1;
|
74 | }
|
75 | }
|
76 |
|
77 | // Fallback for modularized imports:
|
78 | export default getWeekYear;
|