index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import getQuarter from "../getQuarter/index.js";
  2. import toDate from "../toDate/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  4. /**
  5. * @name differenceInCalendarQuarters
  6. * @category Quarter Helpers
  7. * @summary Get the number of calendar quarters between the given dates.
  8. *
  9. * @description
  10. * Get the number of calendar quarters between the given dates.
  11. *
  12. * ### v2.0.0 breaking changes:
  13. *
  14. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  15. *
  16. * @param {Date|Number} dateLeft - the later date
  17. * @param {Date|Number} dateRight - the earlier date
  18. * @returns {Number} the number of calendar quarters
  19. * @throws {TypeError} 2 arguments required
  20. *
  21. * @example
  22. * // How many calendar quarters are between 31 December 2013 and 2 July 2014?
  23. * var result = differenceInCalendarQuarters(
  24. * new Date(2014, 6, 2),
  25. * new Date(2013, 11, 31)
  26. * )
  27. * //=> 3
  28. */
  29. export default function differenceInCalendarQuarters(dirtyDateLeft, dirtyDateRight) {
  30. requiredArgs(2, arguments);
  31. var dateLeft = toDate(dirtyDateLeft);
  32. var dateRight = toDate(dirtyDateRight);
  33. var yearDiff = dateLeft.getFullYear() - dateRight.getFullYear();
  34. var quarterDiff = getQuarter(dateLeft) - getQuarter(dateRight);
  35. return yearDiff * 4 + quarterDiff;
  36. }