setISOWeek.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { getISOWeek } from "./getISOWeek.js";
  2. import { toDate } from "./toDate.js";
  3. /**
  4. * The {@link setISOWeek} function options.
  5. */
  6. /**
  7. * @name setISOWeek
  8. * @category ISO Week Helpers
  9. * @summary Set the ISO week to the given date.
  10. *
  11. * @description
  12. * Set the ISO week to the given date, saving the weekday number.
  13. *
  14. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  15. *
  16. * @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).
  17. * @typeParam ResultDate - The `Date` type of the context function.
  18. *
  19. * @param date - The date to be changed
  20. * @param week - The ISO week of the new date
  21. * @param options - An object with options
  22. *
  23. * @returns The new date with the ISO week set
  24. *
  25. * @example
  26. * // Set the 53rd ISO week to 7 August 2004:
  27. * const result = setISOWeek(new Date(2004, 7, 7), 53)
  28. * //=> Sat Jan 01 2005 00:00:00
  29. */
  30. export function setISOWeek(date, week, options) {
  31. const _date = toDate(date, options?.in);
  32. const diff = getISOWeek(_date, options) - week;
  33. _date.setDate(_date.getDate() - diff * 7);
  34. return _date;
  35. }
  36. // Fallback for modularized imports:
  37. export default setISOWeek;