UNPKG

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