UNPKG

1.15 kBJavaScriptView Raw
1"use strict";
2exports.startOfDecade = startOfDecade;
3var _index = require("./toDate.js");
4
5/**
6 * @name startOfDecade
7 * @category Decade Helpers
8 * @summary Return the start of a decade for the given date.
9 *
10 * @description
11 * Return the start of a decade for 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 original date
16 *
17 * @returns The start of a decade
18 *
19 * @example
20 * // The start of a decade for 21 October 2015 00:00:00:
21 * const result = startOfDecade(new Date(2015, 9, 21, 00, 00, 00))
22 * //=> Jan 01 2010 00:00:00
23 */
24function startOfDecade(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 _date.setFullYear(decade, 0, 1);
32 _date.setHours(0, 0, 0, 0);
33 return _date;
34}