index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import toInteger from "../_lib/toInteger/index.js";
  2. import toDate from "../toDate/index.js";
  3. import startOfISOWeekYear from "../startOfISOWeekYear/index.js";
  4. import differenceInCalendarDays from "../differenceInCalendarDays/index.js";
  5. import requiredArgs from "../_lib/requiredArgs/index.js";
  6. /**
  7. * @name setISOWeekYear
  8. * @category ISO Week-Numbering Year Helpers
  9. * @summary Set the ISO week-numbering year to the given date.
  10. *
  11. * @description
  12. * Set the ISO week-numbering year to the given date,
  13. * saving the week number and the weekday number.
  14. *
  15. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  16. *
  17. * @param {Date|Number} date - the date to be changed
  18. * @param {Number} isoWeekYear - the ISO week-numbering year of the new date
  19. * @returns {Date} the new date with the ISO week-numbering year set
  20. * @throws {TypeError} 2 arguments required
  21. *
  22. * @example
  23. * // Set ISO week-numbering year 2007 to 29 December 2008:
  24. * const result = setISOWeekYear(new Date(2008, 11, 29), 2007)
  25. * //=> Mon Jan 01 2007 00:00:00
  26. */
  27. export default function setISOWeekYear(dirtyDate, dirtyISOWeekYear) {
  28. requiredArgs(2, arguments);
  29. var date = toDate(dirtyDate);
  30. var isoWeekYear = toInteger(dirtyISOWeekYear);
  31. var diff = differenceInCalendarDays(date, startOfISOWeekYear(date));
  32. var fourthOfJanuary = new Date(0);
  33. fourthOfJanuary.setFullYear(isoWeekYear, 0, 4);
  34. fourthOfJanuary.setHours(0, 0, 0, 0);
  35. date = startOfISOWeekYear(fourthOfJanuary);
  36. date.setDate(date.getDate() + diff);
  37. return date;
  38. }