index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var startOfISOWeek = require('../start_of_iso_week/index.js')
  2. var MILLISECONDS_IN_MINUTE = 60000
  3. var MILLISECONDS_IN_WEEK = 604800000
  4. /**
  5. * @category ISO Week Helpers
  6. * @summary Get the number of calendar ISO weeks between the given dates.
  7. *
  8. * @description
  9. * Get the number of calendar ISO weeks between the given dates.
  10. *
  11. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  12. *
  13. * @param {Date|String|Number} dateLeft - the later date
  14. * @param {Date|String|Number} dateRight - the earlier date
  15. * @returns {Number} the number of calendar ISO weeks
  16. *
  17. * @example
  18. * // How many calendar ISO weeks are between 6 July 2014 and 21 July 2014?
  19. * var result = differenceInCalendarISOWeeks(
  20. * new Date(2014, 6, 21),
  21. * new Date(2014, 6, 6)
  22. * )
  23. * //=> 3
  24. */
  25. function differenceInCalendarISOWeeks (dirtyDateLeft, dirtyDateRight) {
  26. var startOfISOWeekLeft = startOfISOWeek(dirtyDateLeft)
  27. var startOfISOWeekRight = startOfISOWeek(dirtyDateRight)
  28. var timestampLeft = startOfISOWeekLeft.getTime() -
  29. startOfISOWeekLeft.getTimezoneOffset() * MILLISECONDS_IN_MINUTE
  30. var timestampRight = startOfISOWeekRight.getTime() -
  31. startOfISOWeekRight.getTimezoneOffset() * MILLISECONDS_IN_MINUTE
  32. // Round the number of days to the nearest integer
  33. // because the number of milliseconds in a week is not constant
  34. // (e.g. it's different in the week of the daylight saving time clock shift)
  35. return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_WEEK)
  36. }
  37. module.exports = differenceInCalendarISOWeeks