UNPKG

1 kBJavaScriptView Raw
1import { toDate } from "./toDate.mjs";
2
3/**
4 * @name getDecade
5 * @category Decade Helpers
6 * @summary Get the decade of the given date.
7 *
8 * @description
9 * Get the decade of 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 given date
14 *
15 * @returns The year of decade
16 *
17 * @example
18 * // Which decade belongs 27 November 1942?
19 * const result = getDecade(new Date(1942, 10, 27))
20 * //=> 1940
21 */
22export function getDecade(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 return decade;
30}
31
32// Fallback for modularized imports:
33export default getDecade;