differenceInQuarters.mjs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import { getRoundingMethod } from "./_lib/getRoundingMethod.mjs";
  2. import { differenceInMonths } from "./differenceInMonths.mjs";
  3. /**
  4. * The {@link differenceInQuarters} function options.
  5. */
  6. /**
  7. * @name differenceInQuarters
  8. * @category Quarter Helpers
  9. * @summary Get the number of quarters between the given dates.
  10. *
  11. * @description
  12. * Get the number of quarters between the given dates.
  13. *
  14. * @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).
  15. *
  16. * @param dateLeft - The later date
  17. * @param dateRight - The earlier date
  18. * @param options - An object with options.
  19. *
  20. * @returns The number of full quarters
  21. *
  22. * @example
  23. * // How many full quarters are between 31 December 2013 and 2 July 2014?
  24. * const result = differenceInQuarters(new Date(2014, 6, 2), new Date(2013, 11, 31))
  25. * //=> 2
  26. */
  27. export function differenceInQuarters(dateLeft, dateRight, options) {
  28. const diff = differenceInMonths(dateLeft, dateRight) / 3;
  29. return getRoundingMethod(options?.roundingMethod)(diff);
  30. }
  31. // Fallback for modularized imports:
  32. export default differenceInQuarters;