index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  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. * @param {Date|Number} date - the original date
  17. * @returns {Date} the end of an ISO week-numbering year
  18. * @throws {TypeError} 1 argument required
  19. *
  20. * @example
  21. * // The end of an ISO week-numbering year for 2 July 2005:
  22. * const result = endOfISOWeekYear(new Date(2005, 6, 2))
  23. * //=> Sun Jan 01 2006 23:59:59.999
  24. */
  25. export default function endOfISOWeekYear(dirtyDate) {
  26. requiredArgs(1, arguments);
  27. var year = getISOWeekYear(dirtyDate);
  28. var fourthOfJanuaryOfNextYear = new Date(0);
  29. fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4);
  30. fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0);
  31. var date = startOfISOWeek(fourthOfJanuaryOfNextYear);
  32. date.setMilliseconds(date.getMilliseconds() - 1);
  33. return date;
  34. }