index.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = previousDay;
  7. var _index = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. var _index2 = _interopRequireDefault(require("../getDay/index.js"));
  9. var _index3 = _interopRequireDefault(require("../subDays/index.js"));
  10. /**
  11. * @name previousDay
  12. * @category Weekday Helpers
  13. * @summary When is the previous day of the week?
  14. *
  15. * @description
  16. * When is the previous day of the week? 0-6 the day of the week, 0 represents Sunday.
  17. *
  18. * @param {Date | number} date - the date to check
  19. * @param {number} day - day of the week
  20. * @returns {Date} - the date is the previous day of week
  21. * @throws {TypeError} - 2 arguments required
  22. *
  23. * @example
  24. * // When is the previous Monday before Mar, 20, 2020?
  25. * const result = previousDay(new Date(2020, 2, 20), 1)
  26. * //=> Mon Mar 16 2020 00:00:00
  27. *
  28. * @example
  29. * // When is the previous Tuesday before Mar, 21, 2020?
  30. * const result = previousDay(new Date(2020, 2, 21), 2)
  31. * //=> Tue Mar 17 2020 00:00:00
  32. */
  33. function previousDay(date, day) {
  34. (0, _index.default)(2, arguments);
  35. var delta = (0, _index2.default)(date) - day;
  36. if (delta <= 0) delta += 7;
  37. return (0, _index3.default)(date, delta);
  38. }
  39. module.exports = exports.default;