lastDayOfDecade.cjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. exports.lastDayOfDecade = lastDayOfDecade;
  3. var _index = require("./toDate.cjs");
  4. /**
  5. * The {@link lastDayOfDecade} function options.
  6. */
  7. /**
  8. * @name lastDayOfDecade
  9. * @category Decade Helpers
  10. * @summary Return the last day of a decade for the given date.
  11. *
  12. * @description
  13. * Return the last day of a decade for the given date.
  14. *
  15. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows using extensions like [`UTCDate`](https://github.com/date-fns/utc).
  16. * @typeParam ResultDate - The result `Date` type; inferred from arguments or specified by context.
  17. *
  18. * @param date - The original date
  19. * @param options - The options
  20. *
  21. * @returns The last day of a decade
  22. *
  23. * @example
  24. * // The last day of a decade for 21 December 2012 21:12:00:
  25. * const result = lastDayOfDecade(new Date(2012, 11, 21, 21, 12, 00))
  26. * //=> Wed Dec 31 2019 00:00:00
  27. */
  28. function lastDayOfDecade(date, options) {
  29. const _date = (0, _index.toDate)(date, options?.in);
  30. const year = _date.getFullYear();
  31. const decade = 9 + Math.floor(year / 10) * 10;
  32. _date.setFullYear(decade + 1, 0, 0);
  33. _date.setHours(0, 0, 0, 0);
  34. return (0, _index.toDate)(_date, options?.in);
  35. }