endOfYear.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { toDate } from "./toDate.js";
  2. /**
  3. * The {@link endOfYear} function options.
  4. */
  5. /**
  6. * @name endOfYear
  7. * @category Year Helpers
  8. * @summary Return the end of a year for the given date.
  9. *
  10. * @description
  11. * Return the end of a year for the given date.
  12. * The result will be in the local timezone.
  13. *
  14. * @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).
  15. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  16. *
  17. * @param date - The original date
  18. * @param options - The options
  19. *
  20. * @returns The end of a year
  21. *
  22. * @example
  23. * // The end of a year for 2 September 2014 11:55:00:
  24. * const result = endOfYear(new Date(2014, 8, 2, 11, 55, 0))
  25. * //=> Wed Dec 31 2014 23:59:59.999
  26. */
  27. export function endOfYear(date, options) {
  28. const _date = toDate(date, options?.in);
  29. const year = _date.getFullYear();
  30. _date.setFullYear(year + 1, 0, 0);
  31. _date.setHours(23, 59, 59, 999);
  32. return _date;
  33. }
  34. // Fallback for modularized imports:
  35. export default endOfYear;