setISODay.mjs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { addDays } from "./addDays.mjs";
  2. import { getISODay } from "./getISODay.mjs";
  3. import { toDate } from "./toDate.mjs";
  4. /**
  5. * @name setISODay
  6. * @category Weekday Helpers
  7. * @summary Set the day of the ISO week to the given date.
  8. *
  9. * @description
  10. * Set the day of the ISO week to the given date.
  11. * ISO week starts with Monday.
  12. * 7 is the index of Sunday, 1 is the index of Monday etc.
  13. *
  14. * @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).
  15. *
  16. * @param date - The date to be changed
  17. * @param day - The day of the ISO week of the new date
  18. *
  19. * @returns The new date with the day of the ISO week set
  20. *
  21. * @example
  22. * // Set Sunday to 1 September 2014:
  23. * const result = setISODay(new Date(2014, 8, 1), 7)
  24. * //=> Sun Sep 07 2014 00:00:00
  25. */
  26. export function setISODay(date, day) {
  27. const _date = toDate(date);
  28. const currentDay = getISODay(_date);
  29. const diff = day - currentDay;
  30. return addDays(_date, diff);
  31. }
  32. // Fallback for modularized imports:
  33. export default setISODay;