startOfDecade.js 1.1 KB

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