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 last day of an ISO week-numbering year for the given date.
  6. *
  7. * @description
  8. * Return the last day 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 last day of an ISO week-numbering year for 2 July 2005:
  19. * var result = lastDayOfISOYear(new Date(2005, 6, 2))
  20. * //=> Sun Jan 01 2006 00:00:00
  21. */
  22. function lastDayOfISOYear (dirtyDate) {
  23. var year = getISOYear(dirtyDate)
  24. var fourthOfJanuary = new Date(0)
  25. fourthOfJanuary.setFullYear(year + 1, 0, 4)
  26. fourthOfJanuary.setHours(0, 0, 0, 0)
  27. var date = startOfISOWeek(fourthOfJanuary)
  28. date.setDate(date.getDate() - 1)
  29. return date
  30. }
  31. module.exports = lastDayOfISOYear