differenceInISOWeekYears.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. exports.differenceInISOWeekYears = differenceInISOWeekYears;
  3. var _index = require("./compareAsc.js");
  4. var _index2 = require("./differenceInCalendarISOWeekYears.js");
  5. var _index3 = require("./subISOWeekYears.js");
  6. var _index4 = require("./toDate.js");
  7. /**
  8. * @name differenceInISOWeekYears
  9. * @category ISO Week-Numbering Year Helpers
  10. * @summary Get the number of full ISO week-numbering years between the given dates.
  11. *
  12. * @description
  13. * Get the number of full ISO week-numbering years between the given dates.
  14. *
  15. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  16. *
  17. * @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).
  18. *
  19. * @param dateLeft - The later date
  20. * @param dateRight - The earlier date
  21. *
  22. * @returns The number of full ISO week-numbering years
  23. *
  24. * @example
  25. * // How many full ISO week-numbering years are between 1 January 2010 and 1 January 2012?
  26. * const result = differenceInISOWeekYears(
  27. * new Date(2012, 0, 1),
  28. * new Date(2010, 0, 1)
  29. * )
  30. * //=> 1
  31. */
  32. function differenceInISOWeekYears(dateLeft, dateRight) {
  33. let _dateLeft = (0, _index4.toDate)(dateLeft);
  34. const _dateRight = (0, _index4.toDate)(dateRight);
  35. const sign = (0, _index.compareAsc)(_dateLeft, _dateRight);
  36. const difference = Math.abs(
  37. (0, _index2.differenceInCalendarISOWeekYears)(_dateLeft, _dateRight),
  38. );
  39. _dateLeft = (0, _index3.subISOWeekYears)(_dateLeft, sign * difference);
  40. // Math.abs(diff in full ISO years - diff in calendar ISO years) === 1
  41. // if last calendar ISO year is not full
  42. // If so, result must be decreased by 1 in absolute value
  43. const isLastISOWeekYearNotFull = Number(
  44. (0, _index.compareAsc)(_dateLeft, _dateRight) === -sign,
  45. );
  46. const result = sign * (difference - isLastISOWeekYearNotFull);
  47. // Prevent negative zero
  48. return result === 0 ? 0 : result;
  49. }