previousDay.js 1.1 KB

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