1 | import { constructFrom } from "./constructFrom.js";
|
2 | import { getISOWeekYear } from "./getISOWeekYear.js";
|
3 | import { startOfISOWeek } from "./startOfISOWeek.js";
|
4 |
|
5 | /**
|
6 | * The {@link startOfISOWeekYear} function options.
|
7 | */
|
8 |
|
9 | /**
|
10 | * @name startOfISOWeekYear
|
11 | * @category ISO Week-Numbering Year Helpers
|
12 | * @summary Return the start of an ISO week-numbering year for the given date.
|
13 | *
|
14 | * @description
|
15 | * Return the start of an ISO week-numbering year,
|
16 | * which always starts 3 days before the year's first Thursday.
|
17 | * The result will be in the local timezone.
|
18 | *
|
19 | * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
|
20 | *
|
21 | * @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).
|
22 | * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
|
23 | *
|
24 | * @param date - The original date
|
25 | * @param options - An object with options
|
26 | *
|
27 | * @returns The start of an ISO week-numbering year
|
28 | *
|
29 | * @example
|
30 | * // The start of an ISO week-numbering year for 2 July 2005:
|
31 | * const result = startOfISOWeekYear(new Date(2005, 6, 2))
|
32 | * //=> Mon Jan 03 2005 00:00:00
|
33 | */
|
34 | export function startOfISOWeekYear(date, options) {
|
35 | const year = getISOWeekYear(date, options);
|
36 | const fourthOfJanuary = constructFrom(options?.in || date, 0);
|
37 | fourthOfJanuary.setFullYear(year, 0, 4);
|
38 | fourthOfJanuary.setHours(0, 0, 0, 0);
|
39 | return startOfISOWeek(fourthOfJanuary);
|
40 | }
|
41 |
|
42 | // Fallback for modularized imports:
|
43 | export default startOfISOWeekYear;
|