UNPKG

1.19 kBJavaScriptView Raw
1import { toDate } from "./toDate.mjs";
2
3/**
4 * @name lastDayOfDecade
5 * @category Decade Helpers
6 * @summary Return the last day of a decade for the given date.
7 *
8 * @description
9 * Return the last day 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 last day of a decade
16 *
17 * @example
18 * // The last day of a decade for 21 December 2012 21:12:00:
19 * const result = lastDayOfDecade(new Date(2012, 11, 21, 21, 12, 00))
20 * //=> Wed Dec 31 2019 00:00:00
21 */
22export function lastDayOfDecade(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 = 9 + Math.floor(year / 10) * 10;
29 _date.setFullYear(decade + 1, 0, 0);
30 _date.setHours(0, 0, 0, 0);
31 return _date;
32}
33
34// Fallback for modularized imports:
35export default lastDayOfDecade;