getQuarter.js 709 B

1234567891011121314151617181920212223242526272829303132
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link getQuarter} function options.
  4. */
  5. /**
  6. * @name getQuarter
  7. * @category Quarter Helpers
  8. * @summary Get the year quarter of the given date.
  9. *
  10. * @description
  11. * Get the year quarter of the given date.
  12. *
  13. * @param date - The given date
  14. * @param options - An object with options
  15. *
  16. * @returns The quarter
  17. *
  18. * @example
  19. * // Which quarter is 2 July 2014?
  20. * const result = getQuarter(new Date(2014, 6, 2));
  21. * //=> 3
  22. */
  23. export function getQuarter(date, options) {
  24. const _date = toDate(date, options?.in);
  25. const quarter = Math.trunc(_date.getMonth() / 3) + 1;
  26. return quarter;
  27. }
  28. // Fallback for modularized imports:
  29. export default getQuarter;