endOfISOWeekYear.cjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. "use strict";
  2. exports.endOfISOWeekYear = endOfISOWeekYear;
  3. var _index = require("./constructFrom.cjs");
  4. var _index2 = require("./getISOWeekYear.cjs");
  5. var _index3 = require("./startOfISOWeek.cjs");
  6. /**
  7. * The {@link endOfISOWeekYear} function options.
  8. */
  9. /**
  10. * @name endOfISOWeekYear
  11. * @category ISO Week-Numbering Year Helpers
  12. * @summary Return the end of an ISO week-numbering year for the given date.
  13. *
  14. * @description
  15. * Return the end of an ISO week-numbering year,
  16. * which always starts 3 days before the year's first Thursday.
  17. * The result will be in the local timezone.
  18. *
  19. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  20. *
  21. * @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).
  22. * @typeParam ContextDate - The `Date` type of the context function.
  23. *
  24. * @param date - The original date
  25. * @param options - The options
  26. *
  27. * @returns The end of an ISO week-numbering year
  28. *
  29. * @example
  30. * // The end of an ISO week-numbering year for 2 July 2005:
  31. * const result = endOfISOWeekYear(new Date(2005, 6, 2))
  32. * //=> Sun Jan 01 2006 23:59:59.999
  33. */
  34. function endOfISOWeekYear(date, options) {
  35. const year = (0, _index2.getISOWeekYear)(date, options);
  36. const fourthOfJanuaryOfNextYear = (0, _index.constructFrom)(
  37. options?.in || date,
  38. 0,
  39. );
  40. fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4);
  41. fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0);
  42. const _date = (0, _index3.startOfISOWeek)(fourthOfJanuaryOfNextYear, options);
  43. _date.setMilliseconds(_date.getMilliseconds() - 1);
  44. return _date;
  45. }