UNPKG

1.18 kBJavaScriptView Raw
1"use strict";
2exports.lastDayOfDecade = lastDayOfDecade;
3var _index = require("./toDate.js");
4
5/**
6 * @name lastDayOfDecade
7 * @category Decade Helpers
8 * @summary Return the last day of a decade for the given date.
9 *
10 * @description
11 * Return the last day 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 last day of a decade
18 *
19 * @example
20 * // The last day of a decade for 21 December 2012 21:12:00:
21 * const result = lastDayOfDecade(new Date(2012, 11, 21, 21, 12, 00))
22 * //=> Wed Dec 31 2019 00:00:00
23 */
24function lastDayOfDecade(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 = 9 + Math.floor(year / 10) * 10;
31 _date.setFullYear(decade + 1, 0, 0);
32 _date.setHours(0, 0, 0, 0);
33 return _date;
34}