differenceInCalendarISOWeeks.cjs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. exports.differenceInCalendarISOWeeks = differenceInCalendarISOWeeks;
  3. var _index = require("./_lib/getTimezoneOffsetInMilliseconds.cjs");
  4. var _index2 = require("./_lib/normalizeDates.cjs");
  5. var _index3 = require("./constants.cjs");
  6. var _index4 = require("./startOfISOWeek.cjs");
  7. /**
  8. * The {@link differenceInCalendarISOWeeks} function options.
  9. */
  10. /**
  11. * @name differenceInCalendarISOWeeks
  12. * @category ISO Week Helpers
  13. * @summary Get the number of calendar ISO weeks between the given dates.
  14. *
  15. * @description
  16. * Get the number of calendar ISO weeks between the given dates.
  17. *
  18. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  19. *
  20. * @param laterDate - The later date
  21. * @param earlierDate - The earlier date
  22. * @param options - An object with options
  23. *
  24. * @returns The number of calendar ISO weeks
  25. *
  26. * @example
  27. * // How many calendar ISO weeks are between 6 July 2014 and 21 July 2014?
  28. * const result = differenceInCalendarISOWeeks(
  29. * new Date(2014, 6, 21),
  30. * new Date(2014, 6, 6),
  31. * );
  32. * //=> 3
  33. */
  34. function differenceInCalendarISOWeeks(laterDate, earlierDate, options) {
  35. const [laterDate_, earlierDate_] = (0, _index2.normalizeDates)(
  36. options?.in,
  37. laterDate,
  38. earlierDate,
  39. );
  40. const startOfISOWeekLeft = (0, _index4.startOfISOWeek)(laterDate_);
  41. const startOfISOWeekRight = (0, _index4.startOfISOWeek)(earlierDate_);
  42. const timestampLeft =
  43. +startOfISOWeekLeft -
  44. (0, _index.getTimezoneOffsetInMilliseconds)(startOfISOWeekLeft);
  45. const timestampRight =
  46. +startOfISOWeekRight -
  47. (0, _index.getTimezoneOffsetInMilliseconds)(startOfISOWeekRight);
  48. // Round the number of weeks to the nearest integer because the number of
  49. // milliseconds in a week is not constant (e.g. it's different in the week of
  50. // the daylight saving time clock shift).
  51. return Math.round(
  52. (timestampLeft - timestampRight) / _index3.millisecondsInWeek,
  53. );
  54. }