endOfISOWeekYear.mjs 1.4 KB

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