setISODay.cjs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. "use strict";
  2. exports.setISODay = setISODay;
  3. var _index = require("./addDays.cjs");
  4. var _index2 = require("./getISODay.cjs");
  5. var _index3 = require("./toDate.cjs");
  6. /**
  7. * The {@link setISODay} function options.
  8. */
  9. /**
  10. * @name setISODay
  11. * @category Weekday Helpers
  12. * @summary Set the day of the ISO week to the given date.
  13. *
  14. * @description
  15. * Set the day of the ISO week to the given date.
  16. * ISO week starts with Monday.
  17. * 7 is the index of Sunday, 1 is the index of Monday, etc.
  18. *
  19. * @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).
  20. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  21. *
  22. * @param date - The date to be changed
  23. * @param day - The day of the ISO week of the new date
  24. * @param options - An object with options
  25. *
  26. * @returns The new date with the day of the ISO week set
  27. *
  28. * @example
  29. * // Set Sunday to 1 September 2014:
  30. * const result = setISODay(new Date(2014, 8, 1), 7)
  31. * //=> Sun Sep 07 2014 00:00:00
  32. */
  33. function setISODay(date, day, options) {
  34. const date_ = (0, _index3.toDate)(date, options?.in);
  35. const currentDay = (0, _index2.getISODay)(date_, options);
  36. const diff = day - currentDay;
  37. return (0, _index.addDays)(date_, diff, options);
  38. }