differenceInCalendarQuarters.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. exports.differenceInCalendarQuarters = differenceInCalendarQuarters;
  3. var _index = require("./getQuarter.js");
  4. var _index2 = require("./toDate.js");
  5. /**
  6. * @name differenceInCalendarQuarters
  7. * @category Quarter Helpers
  8. * @summary Get the number of calendar quarters between the given dates.
  9. *
  10. * @description
  11. * Get the number of calendar quarters between the given dates.
  12. *
  13. * @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).
  14. *
  15. * @param dateLeft - The later date
  16. * @param dateRight - The earlier date
  17. * @returns The number of calendar quarters
  18. *
  19. * @example
  20. * // How many calendar quarters are between 31 December 2013 and 2 July 2014?
  21. * const result = differenceInCalendarQuarters(
  22. * new Date(2014, 6, 2),
  23. * new Date(2013, 11, 31)
  24. * )
  25. * //=> 3
  26. */
  27. function differenceInCalendarQuarters(dateLeft, dateRight) {
  28. const _dateLeft = (0, _index2.toDate)(dateLeft);
  29. const _dateRight = (0, _index2.toDate)(dateRight);
  30. const yearDiff = _dateLeft.getFullYear() - _dateRight.getFullYear();
  31. const quarterDiff =
  32. (0, _index.getQuarter)(_dateLeft) - (0, _index.getQuarter)(_dateRight);
  33. return yearDiff * 4 + quarterDiff;
  34. }