isSameQuarter.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { normalizeDates } from "./_lib/normalizeDates.js";
  2. import { startOfQuarter } from "./startOfQuarter.js";
  3. /**
  4. * The {@link isSameQuarter} function options.
  5. */
  6. /**
  7. * @name isSameQuarter
  8. * @category Quarter Helpers
  9. * @summary Are the given dates in the same quarter (and year)?
  10. *
  11. * @description
  12. * Are the given dates in the same quarter (and year)?
  13. *
  14. * @param laterDate - The first date to check
  15. * @param earlierDate - The second date to check
  16. * @param options - An object with options
  17. *
  18. * @returns The dates are in the same quarter (and year)
  19. *
  20. * @example
  21. * // Are 1 January 2014 and 8 March 2014 in the same quarter?
  22. * const result = isSameQuarter(new Date(2014, 0, 1), new Date(2014, 2, 8))
  23. * //=> true
  24. *
  25. * @example
  26. * // Are 1 January 2014 and 1 January 2015 in the same quarter?
  27. * const result = isSameQuarter(new Date(2014, 0, 1), new Date(2015, 0, 1))
  28. * //=> false
  29. */
  30. export function isSameQuarter(laterDate, earlierDate, options) {
  31. const [dateLeft_, dateRight_] = normalizeDates(
  32. options?.in,
  33. laterDate,
  34. earlierDate,
  35. );
  36. return +startOfQuarter(dateLeft_) === +startOfQuarter(dateRight_);
  37. }
  38. // Fallback for modularized imports:
  39. export default isSameQuarter;