endOfYear.js 924 B

12345678910111213141516171819202122232425262728293031
  1. "use strict";
  2. exports.endOfYear = endOfYear;
  3. var _index = require("./toDate.js");
  4. /**
  5. * @name endOfYear
  6. * @category Year Helpers
  7. * @summary Return the end of a year for the given date.
  8. *
  9. * @description
  10. * Return the end of a year for the given date.
  11. * The result will be in the local timezone.
  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. *
  15. * @param date - The original date
  16. *
  17. * @returns The end of a year
  18. *
  19. * @example
  20. * // The end of a year for 2 September 2014 11:55:00:
  21. * const result = endOfYear(new Date(2014, 8, 2, 11, 55, 00))
  22. * //=> Wed Dec 31 2014 23:59:59.999
  23. */
  24. function endOfYear(date) {
  25. const _date = (0, _index.toDate)(date);
  26. const year = _date.getFullYear();
  27. _date.setFullYear(year + 1, 0, 0);
  28. _date.setHours(23, 59, 59, 999);
  29. return _date;
  30. }