setISODay.js 1.1 KB

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