nextDay.js 1.1 KB

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