index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233
  1. var getISOYear = require('../get_iso_year/index.js')
  2. var startOfISOWeek = require('../start_of_iso_week/index.js')
  3. /**
  4. * @category ISO Week-Numbering Year Helpers
  5. * @summary Return the end of an ISO week-numbering year for the given date.
  6. *
  7. * @description
  8. * Return the end of an ISO week-numbering year,
  9. * which always starts 3 days before the year's first Thursday.
  10. * The result will be in the local timezone.
  11. *
  12. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  13. *
  14. * @param {Date|String|Number} date - the original date
  15. * @returns {Date} the end of an ISO week-numbering year
  16. *
  17. * @example
  18. * // The end of an ISO week-numbering year for 2 July 2005:
  19. * var result = endOfISOYear(new Date(2005, 6, 2))
  20. * //=> Sun Jan 01 2006 23:59:59.999
  21. */
  22. function endOfISOYear (dirtyDate) {
  23. var year = getISOYear(dirtyDate)
  24. var fourthOfJanuaryOfNextYear = new Date(0)
  25. fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4)
  26. fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0)
  27. var date = startOfISOWeek(fourthOfJanuaryOfNextYear)
  28. date.setMilliseconds(date.getMilliseconds() - 1)
  29. return date
  30. }
  31. module.exports = endOfISOYear