1 | import { toDate } from "./toDate.js";
|
2 |
|
3 | /**
|
4 | * The {@link endOfDecade} function options.
|
5 | */
|
6 |
|
7 | /**
|
8 | * @name endOfDecade
|
9 | * @category Decade Helpers
|
10 | * @summary Return the end of a decade for the given date.
|
11 | *
|
12 | * @description
|
13 | * Return the end of a decade for the given date.
|
14 | *
|
15 | * @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).
|
16 | * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
|
17 | *
|
18 | * @param date - The original date
|
19 | * @param options - An object with options
|
20 | *
|
21 | * @returns The end of a decade
|
22 | *
|
23 | * @example
|
24 | * // The end of a decade for 12 May 1984 00:00:00:
|
25 | * const result = endOfDecade(new Date(1984, 4, 12, 00, 00, 00))
|
26 | * //=> Dec 31 1989 23:59:59.999
|
27 | */
|
28 | export function endOfDecade(date, options) {
|
29 | // TODO: Switch to more technical definition in of decades that start with 1
|
30 | // end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
|
31 | // change, so it can only be done in 4.0.
|
32 | const _date = toDate(date, options?.in);
|
33 | const year = _date.getFullYear();
|
34 | const decade = 9 + Math.floor(year / 10) * 10;
|
35 | _date.setFullYear(decade, 11, 31);
|
36 | _date.setHours(23, 59, 59, 999);
|
37 | return _date;
|
38 | }
|
39 |
|
40 | // Fallback for modularized imports:
|
41 | export default endOfDecade;
|