Source: setISOWeek/index.js

  1. import toInteger from '../_lib/toInteger/index'
  2. import toDate from '../toDate/index'
  3. import getISOWeek from '../getISOWeek/index'
  4. import requiredArgs from '../_lib/requiredArgs/index'
  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. * ### v2.0.0 breaking changes:
  16. *
  17. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  18. *
  19. * @param {Date|Number} date - the date to be changed
  20. * @param {Number} isoWeek - the ISO week of the new date
  21. * @returns {Date} the new date with the ISO week set
  22. * @throws {TypeError} 2 arguments required
  23. *
  24. * @example
  25. * // Set the 53rd ISO week to 7 August 2004:
  26. * var result = setISOWeek(new Date(2004, 7, 7), 53)
  27. * //=> Sat Jan 01 2005 00:00:00
  28. */
  29. export default function setISOWeek(dirtyDate, dirtyISOWeek) {
  30. requiredArgs(2, arguments)
  31. var date = toDate(dirtyDate)
  32. var isoWeek = toInteger(dirtyISOWeek)
  33. var diff = getISOWeek(date) - isoWeek
  34. date.setDate(date.getDate() - diff * 7)
  35. return date
  36. }