index.js 955 B

12345678910111213141516171819202122232425262728293031
  1. var parse = require('../parse/index.js')
  2. var addDays = require('../add_days/index.js')
  3. var getISODay = require('../get_iso_day/index.js')
  4. /**
  5. * @category Weekday Helpers
  6. * @summary Set the day of the ISO week to the given date.
  7. *
  8. * @description
  9. * Set the day of the ISO week to the given date.
  10. * ISO week starts with Monday.
  11. * 7 is the index of Sunday, 1 is the index of Monday etc.
  12. *
  13. * @param {Date|String|Number} date - the date to be changed
  14. * @param {Number} day - the day of the ISO week of the new date
  15. * @returns {Date} the new date with the day of the ISO week setted
  16. *
  17. * @example
  18. * // Set Sunday to 1 September 2014:
  19. * var result = setISODay(new Date(2014, 8, 1), 7)
  20. * //=> Sun Sep 07 2014 00:00:00
  21. */
  22. function setISODay (dirtyDate, dirtyDay) {
  23. var date = parse(dirtyDate)
  24. var day = Number(dirtyDay)
  25. var currentDay = getISODay(date)
  26. var diff = day - currentDay
  27. return addDays(date, diff)
  28. }
  29. module.exports = setISODay