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