isSameQuarter.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { startOfQuarter } from "./startOfQuarter.mjs";
  2. /**
  3. * @name isSameQuarter
  4. * @category Quarter Helpers
  5. * @summary Are the given dates in the same quarter (and year)?
  6. *
  7. * @description
  8. * Are the given dates in the same quarter (and year)?
  9. *
  10. * @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).
  11. *
  12. * @param dateLeft - The first date to check
  13. * @param dateRight - The second date to check
  14. * @returns The dates are in the same quarter (and year)
  15. *
  16. * @example
  17. * // Are 1 January 2014 and 8 March 2014 in the same quarter?
  18. * const result = isSameQuarter(new Date(2014, 0, 1), new Date(2014, 2, 8))
  19. * //=> true
  20. *
  21. * @example
  22. * // Are 1 January 2014 and 1 January 2015 in the same quarter?
  23. * const result = isSameQuarter(new Date(2014, 0, 1), new Date(2015, 0, 1))
  24. * //=> false
  25. */
  26. export function isSameQuarter(dateLeft, dateRight) {
  27. const dateLeftStartOfQuarter = startOfQuarter(dateLeft);
  28. const dateRightStartOfQuarter = startOfQuarter(dateRight);
  29. return +dateLeftStartOfQuarter === +dateRightStartOfQuarter;
  30. }
  31. // Fallback for modularized imports:
  32. export default isSameQuarter;