setISOWeek.js 1.0 KB

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