endOfDecade.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. "use strict";
  2. exports.endOfDecade = endOfDecade;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name endOfDecade
  6. * @category Decade Helpers
  7. * @summary Return the end of a decade for the given date.
  8. *
  9. * @description
  10. * Return the end 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 end of a decade
  17. *
  18. * @example
  19. * // The end of a decade for 12 May 1984 00:00:00:
  20. * const result = endOfDecade(new Date(1984, 4, 12, 00, 00, 00))
  21. * //=> Dec 31 1989 23:59:59.999
  22. */
  23. function endOfDecade(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 = 9 + Math.floor(year / 10) * 10;
  30. _date.setFullYear(decade, 11, 31);
  31. _date.setHours(23, 59, 59, 999);
  32. return _date;
  33. }