1 | import { addDays } from "./addDays.js";
|
2 | import { getDay } from "./getDay.js";
|
3 |
|
4 | /**
|
5 | * The {@link nextDay} function options.
|
6 | */
|
7 |
|
8 | /**
|
9 | * @name nextDay
|
10 | * @category Weekday Helpers
|
11 | * @summary When is the next day of the week? 0-6 the day of the week, 0 represents Sunday.
|
12 | *
|
13 | * @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).
|
14 | * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
|
15 | *
|
16 | * @param date - The date to check
|
17 | * @param day - Day of the week
|
18 | * @param options - An object with options
|
19 | *
|
20 | * @returns The date is the next day of the week
|
21 | *
|
22 | * @example
|
23 | * // When is the next Monday after Mar, 20, 2020?
|
24 | * const result = nextDay(new Date(2020, 2, 20), 1)
|
25 | * //=> Mon Mar 23 2020 00:00:00
|
26 | *
|
27 | * @example
|
28 | * // When is the next Tuesday after Mar, 21, 2020?
|
29 | * const result = nextDay(new Date(2020, 2, 21), 2)
|
30 | * //=> Tue Mar 24 2020 00:00:00
|
31 | */
|
32 | export function nextDay(date, day, options) {
|
33 | let delta = day - getDay(date, options);
|
34 | if (delta <= 0) delta += 7;
|
35 |
|
36 | return addDays(date, delta, options);
|
37 | }
|
38 |
|
39 | // Fallback for modularized imports:
|
40 | export default nextDay;
|