index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. var parse = require('../parse/index.js')
  2. var startOfISOWeek = require('../start_of_iso_week/index.js')
  3. var startOfISOYear = require('../start_of_iso_year/index.js')
  4. var MILLISECONDS_IN_WEEK = 604800000
  5. /**
  6. * @category ISO Week Helpers
  7. * @summary Get the ISO week of the given date.
  8. *
  9. * @description
  10. * Get the ISO week of the given date.
  11. *
  12. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  13. *
  14. * @param {Date|String|Number} date - the given date
  15. * @returns {Number} the ISO week
  16. *
  17. * @example
  18. * // Which week of the ISO-week numbering year is 2 January 2005?
  19. * var result = getISOWeek(new Date(2005, 0, 2))
  20. * //=> 53
  21. */
  22. function getISOWeek (dirtyDate) {
  23. var date = parse(dirtyDate)
  24. var diff = startOfISOWeek(date).getTime() - startOfISOYear(date).getTime()
  25. // Round the number of days 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) + 1
  29. }
  30. module.exports = getISOWeek