setISOWeek.cjs 1.2 KB

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