lastDayOfDecade.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link lastDayOfDecade} function options.
  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 using extensions like [`UTCDate`](https://github.com/date-fns/utc).
  14. * @typeParam ResultDate - The result `Date` type; inferred from arguments or specified by context.
  15. *
  16. * @param date - The original date
  17. * @param options - The options
  18. *
  19. * @returns The last day of a decade
  20. *
  21. * @example
  22. * // The last day of a decade for 21 December 2012 21:12:00:
  23. * const result = lastDayOfDecade(new Date(2012, 11, 21, 21, 12, 00))
  24. * //=> Wed Dec 31 2019 00:00:00
  25. */
  26. export function lastDayOfDecade(date, options) {
  27. const _date = toDate(date, options?.in);
  28. const year = _date.getFullYear();
  29. const decade = 9 + Math.floor(year / 10) * 10;
  30. _date.setFullYear(decade + 1, 0, 0);
  31. _date.setHours(0, 0, 0, 0);
  32. return toDate(_date, options?.in);
  33. }
  34. // Fallback for modularized imports:
  35. export default lastDayOfDecade;