index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. import toInteger from "../_lib/toInteger/index.js";
  2. import toDate from "../toDate/index.js";
  3. import getISOWeek from "../getISOWeek/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.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. * @param {Date|Number} date - the date to be changed
  16. * @param {Number} isoWeek - the ISO week of the new date
  17. * @returns {Date} the new date with the ISO week set
  18. * @throws {TypeError} 2 arguments required
  19. *
  20. * @example
  21. * // Set the 53rd ISO week to 7 August 2004:
  22. * const result = setISOWeek(new Date(2004, 7, 7), 53)
  23. * //=> Sat Jan 01 2005 00:00:00
  24. */
  25. export default function setISOWeek(dirtyDate, dirtyISOWeek) {
  26. requiredArgs(2, arguments);
  27. var date = toDate(dirtyDate);
  28. var isoWeek = toInteger(dirtyISOWeek);
  29. var diff = getISOWeek(date) - isoWeek;
  30. date.setDate(date.getDate() - diff * 7);
  31. return date;
  32. }