UNPKG

940 BJavaScriptView Raw
1import { toDate } from "./toDate.mjs";
2
3/**
4 * @name endOfYear
5 * @category Year Helpers
6 * @summary Return the end of a year for the given date.
7 *
8 * @description
9 * Return the end of a year 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 year
17 *
18 * @example
19 * // The end of a year for 2 September 2014 11:55:00:
20 * const result = endOfYear(new Date(2014, 8, 2, 11, 55, 00))
21 * //=> Wed Dec 31 2014 23:59:59.999
22 */
23export function endOfYear(date) {
24 const _date = toDate(date);
25 const year = _date.getFullYear();
26 _date.setFullYear(year + 1, 0, 0);
27 _date.setHours(23, 59, 59, 999);
28 return _date;
29}
30
31// Fallback for modularized imports:
32export default endOfYear;