index.js 908 B

123456789101112131415161718192021222324252627282930
  1. var parse = require('../parse/index.js')
  2. var getISOWeek = require('../get_iso_week/index.js')
  3. /**
  4. * @category ISO Week Helpers
  5. * @summary Set the ISO week to the given date.
  6. *
  7. * @description
  8. * Set the ISO week to the given date, saving the weekday number.
  9. *
  10. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  11. *
  12. * @param {Date|String|Number} date - the date to be changed
  13. * @param {Number} isoWeek - the ISO week of the new date
  14. * @returns {Date} the new date with the ISO week setted
  15. *
  16. * @example
  17. * // Set the 53rd ISO week to 7 August 2004:
  18. * var result = setISOWeek(new Date(2004, 7, 7), 53)
  19. * //=> Sat Jan 01 2005 00:00:00
  20. */
  21. function setISOWeek (dirtyDate, dirtyISOWeek) {
  22. var date = parse(dirtyDate)
  23. var isoWeek = Number(dirtyISOWeek)
  24. var diff = getISOWeek(date) - isoWeek
  25. date.setDate(date.getDate() - diff * 7)
  26. return date
  27. }
  28. module.exports = setISOWeek