1 | ;
|
2 | exports.lastDayOfWeek = lastDayOfWeek;
|
3 | var _index = require("./toDate.js");
|
4 |
|
5 | var _index2 = require("./_lib/defaultOptions.js");
|
6 |
|
7 | /**
|
8 | * The {@link lastDayOfWeek} function options.
|
9 | */
|
10 |
|
11 | /**
|
12 | * @name lastDayOfWeek
|
13 | * @category Week Helpers
|
14 | * @summary Return the last day of a week for the given date.
|
15 | *
|
16 | * @description
|
17 | * Return the last day of a week for the given date.
|
18 | * The result will be in the local timezone.
|
19 | *
|
20 | * @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).
|
21 | *
|
22 | * @param date - The original date
|
23 | * @param options - An object with options
|
24 | *
|
25 | * @returns The last day of a week
|
26 | *
|
27 | * @example
|
28 | * // The last day of a week for 2 September 2014 11:55:00:
|
29 | * const result = lastDayOfWeek(new Date(2014, 8, 2, 11, 55, 0))
|
30 | * //=> Sat Sep 06 2014 00:00:00
|
31 | *
|
32 | * @example
|
33 | * // If the week starts on Monday, the last day of the week for 2 September 2014 11:55:00:
|
34 | * const result = lastDayOfWeek(new Date(2014, 8, 2, 11, 55, 0), { weekStartsOn: 1 })
|
35 | * //=> Sun Sep 07 2014 00:00:00
|
36 | */
|
37 | function lastDayOfWeek(date, options) {
|
38 | const defaultOptions = (0, _index2.getDefaultOptions)();
|
39 | const weekStartsOn =
|
40 | options?.weekStartsOn ??
|
41 | options?.locale?.options?.weekStartsOn ??
|
42 | defaultOptions.weekStartsOn ??
|
43 | defaultOptions.locale?.options?.weekStartsOn ??
|
44 | 0;
|
45 |
|
46 | const _date = (0, _index.toDate)(date);
|
47 | const day = _date.getDay();
|
48 | const diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
|
49 |
|
50 | _date.setHours(0, 0, 0, 0);
|
51 | _date.setDate(_date.getDate() + diff);
|
52 | return _date;
|
53 | }
|