35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
import toDate from '../toDate/index.js';
|
|
/**
|
|
* @name startOfDecade
|
|
* @category Decade Helpers
|
|
* @summary Return the start of a decade for the given date.
|
|
*
|
|
* @description
|
|
* Return the start of a decade for the given date.
|
|
*
|
|
* ### v2.0.0 breaking changes:
|
|
*
|
|
* - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
|
|
*
|
|
* @param {Date|Number} date - the original date
|
|
* @returns {Date} the start of a decade
|
|
* @throws {TypeError} 1 argument required
|
|
*
|
|
* @example
|
|
* // The start of a decade for 21 October 2015 00:00:00:
|
|
* var result = startOfDecade(new Date(2015, 9, 21, 00, 00, 00))
|
|
* //=> Jan 01 2010 00:00:00
|
|
*/
|
|
|
|
export default function startOfDecade(dirtyDate) {
|
|
if (arguments.length < 1) {
|
|
throw new TypeError('1 argument required, but only ' + arguments.length + ' present');
|
|
}
|
|
|
|
var date = toDate(dirtyDate);
|
|
var year = date.getFullYear();
|
|
var decade = Math.floor(year / 10) * 10;
|
|
date.setFullYear(decade, 0, 1);
|
|
date.setHours(0, 0, 0, 0);
|
|
return date;
|
|
} |