index.js 757 B

123456789101112131415161718192021222324252627
  1. var parse = require('../parse/index.js')
  2. /**
  3. * @category Day Helpers
  4. * @summary Set the day of the year to the given date.
  5. *
  6. * @description
  7. * Set the day of the year to the given date.
  8. *
  9. * @param {Date|String|Number} date - the date to be changed
  10. * @param {Number} dayOfYear - the day of the year of the new date
  11. * @returns {Date} the new date with the day of the year setted
  12. *
  13. * @example
  14. * // Set the 2nd day of the year to 2 July 2014:
  15. * var result = setDayOfYear(new Date(2014, 6, 2), 2)
  16. * //=> Thu Jan 02 2014 00:00:00
  17. */
  18. function setDayOfYear (dirtyDate, dirtyDayOfYear) {
  19. var date = parse(dirtyDate)
  20. var dayOfYear = Number(dirtyDayOfYear)
  21. date.setMonth(0)
  22. date.setDate(dayOfYear)
  23. return date
  24. }
  25. module.exports = setDayOfYear