lastDayOfYear.cjs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. "use strict";
  2. exports.lastDayOfYear = lastDayOfYear;
  3. var _index = require("./toDate.cjs");
  4. /**
  5. * The {@link lastDayOfYear} function options.
  6. */
  7. /**
  8. * @name lastDayOfYear
  9. * @category Year Helpers
  10. * @summary Return the last day of a year for the given date.
  11. *
  12. * @description
  13. * Return the last day of a year for the given date.
  14. * The result will be in the local timezone.
  15. *
  16. * @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).
  17. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  18. *
  19. * @param date - The original date
  20. * @param options - An object with options
  21. *
  22. * @returns The last day of a year
  23. *
  24. * @example
  25. * // The last day of a year for 2 September 2014 11:55:00:
  26. * const result = lastDayOfYear(new Date(2014, 8, 2, 11, 55, 00))
  27. * //=> Wed Dec 31 2014 00:00:00
  28. */
  29. function lastDayOfYear(date, options) {
  30. const date_ = (0, _index.toDate)(date, options?.in);
  31. const year = date_.getFullYear();
  32. date_.setFullYear(year + 1, 0, 0);
  33. date_.setHours(0, 0, 0, 0);
  34. return date_;
  35. }