getQuarter.mjs 775 B

1234567891011121314151617181920212223242526272829
  1. import { toDate } from "./toDate.mjs";
  2. /**
  3. * @name getQuarter
  4. * @category Quarter Helpers
  5. * @summary Get the year quarter of the given date.
  6. *
  7. * @description
  8. * Get the year quarter of the given date.
  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 date - The given date
  13. *
  14. * @returns The quarter
  15. *
  16. * @example
  17. * // Which quarter is 2 July 2014?
  18. * const result = getQuarter(new Date(2014, 6, 2))
  19. * //=> 3
  20. */
  21. export function getQuarter(date) {
  22. const _date = toDate(date);
  23. const quarter = Math.trunc(_date.getMonth() / 3) + 1;
  24. return quarter;
  25. }
  26. // Fallback for modularized imports:
  27. export default getQuarter;