endOfISOWeekYear.js 1.6 KB

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