endOfDecade.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link endOfDecade} function options.
  4. */
  5. /**
  6. * @name endOfDecade
  7. * @category Decade Helpers
  8. * @summary Return the end of a decade for the given date.
  9. *
  10. * @description
  11. * Return the end of a decade for the given date.
  12. *
  13. * @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).
  14. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  15. *
  16. * @param date - The original date
  17. * @param options - An object with options
  18. *
  19. * @returns The end of a decade
  20. *
  21. * @example
  22. * // The end of a decade for 12 May 1984 00:00:00:
  23. * const result = endOfDecade(new Date(1984, 4, 12, 00, 00, 00))
  24. * //=> Dec 31 1989 23:59:59.999
  25. */
  26. export function endOfDecade(date, options) {
  27. // TODO: Switch to more technical definition in of decades that start with 1
  28. // end with 0. I.e. 2001-2010 instead of current 2000-2009. It's a breaking
  29. // change, so it can only be done in 4.0.
  30. const _date = toDate(date, options?.in);
  31. const year = _date.getFullYear();
  32. const decade = 9 + Math.floor(year / 10) * 10;
  33. _date.setFullYear(decade, 11, 31);
  34. _date.setHours(23, 59, 59, 999);
  35. return _date;
  36. }
  37. // Fallback for modularized imports:
  38. export default endOfDecade;