UNPKG

1.06 kBJavaScriptView Raw
1"use strict";
2exports.setISOWeek = setISOWeek;
3var _index = require("./getISOWeek.js");
4var _index2 = require("./toDate.js");
5
6/**
7 * @name setISOWeek
8 * @category ISO Week Helpers
9 * @summary Set the ISO week to the given date.
10 *
11 * @description
12 * Set the ISO week to the given date, saving the weekday number.
13 *
14 * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
15 *
16 * @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).
17 *
18 * @param date - The date to be changed
19 * @param week - The ISO week of the new date
20 *
21 * @returns The new date with the ISO week set
22 *
23 * @example
24 * // Set the 53rd ISO week to 7 August 2004:
25 * const result = setISOWeek(new Date(2004, 7, 7), 53)
26 * //=> Sat Jan 01 2005 00:00:00
27 */
28function setISOWeek(date, week) {
29 const _date = (0, _index2.toDate)(date);
30 const diff = (0, _index.getISOWeek)(_date) - week;
31 _date.setDate(_date.getDate() - diff * 7);
32 return _date;
33}