nextDay.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { addDays } from "./addDays.js";
  2. import { getDay } from "./getDay.js";
  3. /**
  4. * The {@link nextDay} function options.
  5. */
  6. /**
  7. * @name nextDay
  8. * @category Weekday Helpers
  9. * @summary When is the next day of the week? 0-6 the day of the week, 0 represents Sunday.
  10. *
  11. * @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).
  12. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  13. *
  14. * @param date - The date to check
  15. * @param day - Day of the week
  16. * @param options - An object with options
  17. *
  18. * @returns The date is the next day of the 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. export function nextDay(date, day, options) {
  31. let delta = day - getDay(date, options);
  32. if (delta <= 0) delta += 7;
  33. return addDays(date, delta, options);
  34. }
  35. // Fallback for modularized imports:
  36. export default nextDay;