index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import getISOWeekYear from "../getISOWeekYear/index.js";
  2. import startOfISOWeek from "../startOfISOWeek/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  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. * ### v2.0.0 breaking changes:
  17. *
  18. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  19. *
  20. * - The function was renamed from `endOfISOYear` to `endOfISOWeekYear`.
  21. * "ISO week year" is short for [ISO week-numbering year](https://en.wikipedia.org/wiki/ISO_week_date).
  22. * This change makes the name consistent with
  23. * locale-dependent week-numbering year helpers, e.g., `addWeekYears`.
  24. *
  25. * @param {Date|Number} date - the original date
  26. * @returns {Date} the end of an ISO week-numbering year
  27. * @throws {TypeError} 1 argument required
  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. export default function endOfISOWeekYear(dirtyDate) {
  35. requiredArgs(1, arguments);
  36. var year = getISOWeekYear(dirtyDate);
  37. var fourthOfJanuaryOfNextYear = new Date(0);
  38. fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4);
  39. fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0);
  40. var date = startOfISOWeek(fourthOfJanuaryOfNextYear);
  41. date.setMilliseconds(date.getMilliseconds() - 1);
  42. return date;
  43. }