index.js 1010 B

1234567891011121314151617181920212223242526272829303132
  1. import addDays from "../addDays/index.js";
  2. import getDay from "../getDay/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  4. /**
  5. * @name nextDay
  6. * @category Weekday Helpers
  7. * @summary When is the next day of the week?
  8. *
  9. * @description
  10. * When is the next day of the week? 0-6 the day of the week, 0 represents Sunday.
  11. *
  12. * @param {Date | number} date - the date to check
  13. * @param {Day} day - day of the week
  14. * @returns {Date} - the date is the next day of week
  15. * @throws {TypeError} - 2 arguments required
  16. *
  17. * @example
  18. * // When is the next Monday after Mar, 20, 2020?
  19. * const result = nextDay(new Date(2020, 2, 20), 1)
  20. * //=> Mon Mar 23 2020 00:00:00
  21. *
  22. * @example
  23. * // When is the next Tuesday after Mar, 21, 2020?
  24. * const result = nextDay(new Date(2020, 2, 21), 2)
  25. * //=> Tue Mar 24 2020 00:00:00
  26. */
  27. export default function nextDay(date, day) {
  28. requiredArgs(2, arguments);
  29. var delta = day - getDay(date);
  30. if (delta <= 0) delta += 7;
  31. return addDays(date, delta);
  32. }