1 | import { getDefaultOptions } from "./_lib/defaultOptions.js";
|
2 | import { constructFrom } from "./constructFrom.js";
|
3 | import { differenceInCalendarDays } from "./differenceInCalendarDays.js";
|
4 | import { startOfWeekYear } from "./startOfWeekYear.js";
|
5 | import { toDate } from "./toDate.js";
|
6 |
|
7 | /**
|
8 | * The {@link setWeekYear} function options.
|
9 | */
|
10 |
|
11 | /**
|
12 | * @name setWeekYear
|
13 | * @category Week-Numbering Year Helpers
|
14 | * @summary Set the local week-numbering year to the given date.
|
15 | *
|
16 | * @description
|
17 | * Set the local week-numbering year to the given date,
|
18 | * saving the week number and the weekday number.
|
19 | * The exact calculation depends on the values of
|
20 | * `options.weekStartsOn` (which is the index of the first day of the week)
|
21 | * and `options.firstWeekContainsDate` (which is the day of January, which is always in
|
22 | * the first week of the week-numbering year)
|
23 | *
|
24 | * Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
|
25 | *
|
26 | * @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).
|
27 | * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
|
28 | *
|
29 | * @param date - The date to be changed
|
30 | * @param weekYear - The local week-numbering year of the new date
|
31 | * @param options - An object with options
|
32 | *
|
33 | * @returns The new date with the local week-numbering year set
|
34 | *
|
35 | * @example
|
36 | * // Set the local week-numbering year 2004 to 2 January 2010 with default options:
|
37 | * const result = setWeekYear(new Date(2010, 0, 2), 2004)
|
38 | * //=> Sat Jan 03 2004 00:00:00
|
39 | *
|
40 | * @example
|
41 | * // Set the local week-numbering year 2004 to 2 January 2010,
|
42 | * // if Monday is the first day of week
|
43 | * // and 4 January is always in the first week of the year:
|
44 | * const result = setWeekYear(new Date(2010, 0, 2), 2004, {
|
45 | * weekStartsOn: 1,
|
46 | * firstWeekContainsDate: 4
|
47 | * })
|
48 | * //=> Sat Jan 01 2005 00:00:00
|
49 | */
|
50 | export function setWeekYear(date, weekYear, options) {
|
51 | const defaultOptions = getDefaultOptions();
|
52 | const firstWeekContainsDate =
|
53 | options?.firstWeekContainsDate ??
|
54 | options?.locale?.options?.firstWeekContainsDate ??
|
55 | defaultOptions.firstWeekContainsDate ??
|
56 | defaultOptions.locale?.options?.firstWeekContainsDate ??
|
57 | 1;
|
58 |
|
59 | const diff = differenceInCalendarDays(
|
60 | toDate(date, options?.in),
|
61 | startOfWeekYear(date, options),
|
62 | options,
|
63 | );
|
64 |
|
65 | const firstWeek = constructFrom(options?.in || date, 0);
|
66 | firstWeek.setFullYear(weekYear, 0, firstWeekContainsDate);
|
67 | firstWeek.setHours(0, 0, 0, 0);
|
68 |
|
69 | const date_ = startOfWeekYear(firstWeek, options);
|
70 | date_.setDate(date_.getDate() + diff);
|
71 | return date_;
|
72 | }
|
73 |
|
74 | // Fallback for modularized imports:
|
75 | export default setWeekYear;
|