UNPKG

1.03 kBJavaScriptView Raw
1import { toDate } from "./toDate.mjs";
2
3/**
4 * @name endOfQuarter
5 * @category Quarter Helpers
6 * @summary Return the end of a year quarter for the given date.
7 *
8 * @description
9 * Return the end of a year quarter for the given date.
10 * The result will be in the local timezone.
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 original date
15 *
16 * @returns The end of a quarter
17 *
18 * @example
19 * // The end of a quarter for 2 September 2014 11:55:00:
20 * const result = endOfQuarter(new Date(2014, 8, 2, 11, 55, 0))
21 * //=> Tue Sep 30 2014 23:59:59.999
22 */
23export function endOfQuarter(date) {
24 const _date = toDate(date);
25 const currentMonth = _date.getMonth();
26 const month = currentMonth - (currentMonth % 3) + 3;
27 _date.setMonth(month, 0);
28 _date.setHours(23, 59, 59, 999);
29 return _date;
30}
31
32// Fallback for modularized imports:
33export default endOfQuarter;