UNPKG

1.13 kBJavaScriptView Raw
1"use strict";
2exports.previousDay = previousDay;
3var _index = require("./getDay.js");
4var _index2 = require("./subDays.js");
5
6/**
7 * @name previousDay
8 * @category Weekday Helpers
9 * @summary When is the previous day of the week?
10 *
11 * @description
12 * When is the previous day of the week? 0-6 the day of the week, 0 represents Sunday.
13 *
14 * @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).
15 *
16 * @param date - The date to check
17 * @param day - The day of the week
18 *
19 * @returns The date is the previous day of week
20 *
21 * @example
22 * // When is the previous Monday before Mar, 20, 2020?
23 * const result = previousDay(new Date(2020, 2, 20), 1)
24 * //=> Mon Mar 16 2020 00:00:00
25 *
26 * @example
27 * // When is the previous Tuesday before Mar, 21, 2020?
28 * const result = previousDay(new Date(2020, 2, 21), 2)
29 * //=> Tue Mar 17 2020 00:00:00
30 */
31function previousDay(date, day) {
32 let delta = (0, _index.getDay)(date) - day;
33 if (delta <= 0) delta += 7;
34
35 return (0, _index2.subDays)(date, delta);
36}