previousDay.cjs 1.4 KB

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