1 | import { getISOWeek } from "./getISOWeek.js";
|
2 | import { toDate } from "./toDate.js";
|
3 |
|
4 | /**
|
5 | * The {@link setISOWeek} function options.
|
6 | */
|
7 |
|
8 | /**
|
9 | * @name setISOWeek
|
10 | * @category ISO Week Helpers
|
11 | * @summary Set the ISO week to the given date.
|
12 | *
|
13 | * @description
|
14 | * Set the ISO week to the given date, saving the weekday number.
|
15 | *
|
16 | * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
|
17 | *
|
18 | * @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).
|
19 | * @typeParam ResultDate - The `Date` type of the context function.
|
20 | *
|
21 | * @param date - The date to be changed
|
22 | * @param week - The ISO week of the new date
|
23 | * @param options - An object with options
|
24 | *
|
25 | * @returns The new date with the ISO week set
|
26 | *
|
27 | * @example
|
28 | * // Set the 53rd ISO week to 7 August 2004:
|
29 | * const result = setISOWeek(new Date(2004, 7, 7), 53)
|
30 | * //=> Sat Jan 01 2005 00:00:00
|
31 | */
|
32 | export function setISOWeek(date, week, options) {
|
33 | const _date = toDate(date, options?.in);
|
34 | const diff = getISOWeek(_date, options) - week;
|
35 | _date.setDate(_date.getDate() - diff * 7);
|
36 | return _date;
|
37 | }
|
38 |
|
39 | // Fallback for modularized imports:
|
40 | export default setISOWeek;
|