index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. var startOfISOYear = require('../start_of_iso_year/index.js')
  2. var addWeeks = require('../add_weeks/index.js')
  3. var MILLISECONDS_IN_WEEK = 604800000
  4. /**
  5. * @category ISO Week-Numbering Year Helpers
  6. * @summary Get the number of weeks in an ISO week-numbering year of the given date.
  7. *
  8. * @description
  9. * Get the number of weeks in an ISO week-numbering year of the given date.
  10. *
  11. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  12. *
  13. * @param {Date|String|Number} date - the given date
  14. * @returns {Number} the number of ISO weeks in a year
  15. *
  16. * @example
  17. * // How many weeks are in ISO week-numbering year 2015?
  18. * var result = getISOWeeksInYear(new Date(2015, 1, 11))
  19. * //=> 53
  20. */
  21. function getISOWeeksInYear (dirtyDate) {
  22. var thisYear = startOfISOYear(dirtyDate)
  23. var nextYear = startOfISOYear(addWeeks(thisYear, 60))
  24. var diff = nextYear.valueOf() - thisYear.valueOf()
  25. // Round the number of weeks to the nearest integer
  26. // because the number of milliseconds in a week is not constant
  27. // (e.g. it's different in the week of the daylight saving time clock shift)
  28. return Math.round(diff / MILLISECONDS_IN_WEEK)
  29. }
  30. module.exports = getISOWeeksInYear