index.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  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 start of an ISO week-numbering year for the given date.
  6. *
  7. * @description
  8. * Return the start 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 start of an ISO year
  16. *
  17. * @example
  18. * // The start of an ISO week-numbering year for 2 July 2005:
  19. * var result = startOfISOYear(new Date(2005, 6, 2))
  20. * //=> Mon Jan 03 2005 00:00:00
  21. */
  22. function startOfISOYear (dirtyDate) {
  23. var year = getISOYear(dirtyDate)
  24. var fourthOfJanuary = new Date(0)
  25. fourthOfJanuary.setFullYear(year, 0, 4)
  26. fourthOfJanuary.setHours(0, 0, 0, 0)
  27. var date = startOfISOWeek(fourthOfJanuary)
  28. return date
  29. }
  30. module.exports = startOfISOYear