differenceInCalendarISOWeeks.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. "use strict";
  2. exports.differenceInCalendarISOWeeks = differenceInCalendarISOWeeks;
  3. var _index = require("./constants.js");
  4. var _index2 = require("./startOfISOWeek.js");
  5. var _index3 = require("./_lib/getTimezoneOffsetInMilliseconds.js");
  6. /**
  7. * @name differenceInCalendarISOWeeks
  8. * @category ISO Week Helpers
  9. * @summary Get the number of calendar ISO weeks between the given dates.
  10. *
  11. * @description
  12. * Get the number of calendar ISO weeks between the given dates.
  13. *
  14. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  15. *
  16. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  17. *
  18. * @param dateLeft - The later date
  19. * @param dateRight - The earlier date
  20. *
  21. * @returns The number of calendar ISO weeks
  22. *
  23. * @example
  24. * // How many calendar ISO weeks are between 6 July 2014 and 21 July 2014?
  25. * const result = differenceInCalendarISOWeeks(
  26. * new Date(2014, 6, 21),
  27. * new Date(2014, 6, 6)
  28. * )
  29. * //=> 3
  30. */
  31. function differenceInCalendarISOWeeks(dateLeft, dateRight) {
  32. const startOfISOWeekLeft = (0, _index2.startOfISOWeek)(dateLeft);
  33. const startOfISOWeekRight = (0, _index2.startOfISOWeek)(dateRight);
  34. const timestampLeft =
  35. +startOfISOWeekLeft -
  36. (0, _index3.getTimezoneOffsetInMilliseconds)(startOfISOWeekLeft);
  37. const timestampRight =
  38. +startOfISOWeekRight -
  39. (0, _index3.getTimezoneOffsetInMilliseconds)(startOfISOWeekRight);
  40. // Round the number of weeks to the nearest integer because the number of
  41. // milliseconds in a week is not constant (e.g. it's different in the week of
  42. // the daylight saving time clock shift).
  43. return Math.round(
  44. (timestampLeft - timestampRight) / _index.millisecondsInWeek,
  45. );
  46. }