1 | import { toDate } from "./toDate.mjs";
|
2 |
|
3 | /**
|
4 | * @name startOfDecade
|
5 | * @category Decade Helpers
|
6 | * @summary Return the start of a decade for the given date.
|
7 | *
|
8 | * @description
|
9 | * Return the start of a decade for 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 original date
|
14 | *
|
15 | * @returns The start of a decade
|
16 | *
|
17 | * @example
|
18 | * // The start of a decade for 21 October 2015 00:00:00:
|
19 | * const result = startOfDecade(new Date(2015, 9, 21, 00, 00, 00))
|
20 | * //=> Jan 01 2010 00:00:00
|
21 | */
|
22 | export function startOfDecade(date) {
|
23 | // TODO: Switch to more technical definition in of decades that start with 1
|
24 | // end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
|
25 | // change, so it can only be done in 4.0.
|
26 | const _date = toDate(date);
|
27 | const year = _date.getFullYear();
|
28 | const decade = Math.floor(year / 10) * 10;
|
29 | _date.setFullYear(decade, 0, 1);
|
30 | _date.setHours(0, 0, 0, 0);
|
31 | return _date;
|
32 | }
|
33 |
|
34 | // Fallback for modularized imports:
|
35 | export default startOfDecade;
|