1 | import { getDefaultOptions } from "./_lib/defaultOptions.js";
|
2 | import { toDate } from "./toDate.js";
|
3 |
|
4 | /**
|
5 | * The {@link lastDayOfWeek} function options.
|
6 | */
|
7 |
|
8 | /**
|
9 | * @name lastDayOfWeek
|
10 | * @category Week Helpers
|
11 | * @summary Return the last day of a week for the given date.
|
12 | *
|
13 | * @description
|
14 | * Return the last day of a week for the given date.
|
15 | * The result will be in the local timezone unless a context is specified.
|
16 | *
|
17 | * @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).
|
18 | * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
|
19 | *
|
20 | * @param date - The original date
|
21 | * @param options - An object with options
|
22 | *
|
23 | * @returns The last day of a week
|
24 | */
|
25 | export function lastDayOfWeek(date, options) {
|
26 | const defaultOptions = getDefaultOptions();
|
27 | const weekStartsOn =
|
28 | options?.weekStartsOn ??
|
29 | options?.locale?.options?.weekStartsOn ??
|
30 | defaultOptions.weekStartsOn ??
|
31 | defaultOptions.locale?.options?.weekStartsOn ??
|
32 | 0;
|
33 |
|
34 | const _date = toDate(date, options?.in);
|
35 | const day = _date.getDay();
|
36 | const diff = (day < weekStartsOn ? -7 : 0) + 6 - (day - weekStartsOn);
|
37 |
|
38 | _date.setHours(0, 0, 0, 0);
|
39 | _date.setDate(_date.getDate() + diff);
|
40 |
|
41 | return _date;
|
42 | }
|
43 |
|
44 | // Fallback for modularized imports:
|
45 | export default lastDayOfWeek;
|