UNPKG

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