getQuarter.js 760 B

12345678910111213141516171819202122232425262728
  1. "use strict";
  2. exports.getQuarter = getQuarter;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name getQuarter
  6. * @category Quarter Helpers
  7. * @summary Get the year quarter of the given date.
  8. *
  9. * @description
  10. * Get the year quarter of the given date.
  11. *
  12. * @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).
  13. *
  14. * @param date - The given date
  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. function getQuarter(date) {
  24. const _date = (0, _index.toDate)(date);
  25. const quarter = Math.trunc(_date.getMonth() / 3) + 1;
  26. return quarter;
  27. }