UNPKG

936 BJavaScriptView Raw
1import { toDate } from "./toDate.js";
2
3/**
4 * The {@link getDecade} function options.
5 */
6
7/**
8 * @name getDecade
9 * @category Decade Helpers
10 * @summary Get the decade of the given date.
11 *
12 * @description
13 * Get the decade of the given date.
14 *
15 * @param date - The given date
16 * @param options - An object with options
17 *
18 * @returns The year of decade
19 *
20 * @example
21 * // Which decade belongs 27 November 1942?
22 * const result = getDecade(new Date(1942, 10, 27))
23 * //=> 1940
24 */
25export function getDecade(date, options) {
26 // TODO: Switch to more technical definition in of decades that start with 1
27 // end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
28 // change, so it can only be done in 4.0.
29 const _date = toDate(date, options?.in);
30 const year = _date.getFullYear();
31 const decade = Math.floor(year / 10) * 10;
32 return decade;
33}
34
35// Fallback for modularized imports:
36export default getDecade;