index.js 1.2 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 lastDayOfISOWeekYear
  6. * @category ISO Week-Numbering Year Helpers
  7. * @summary Return the last day of an ISO week-numbering year for the given date.
  8. *
  9. * @description
  10. * Return the last day 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 last day of an ISO week-numbering year for 2 July 2005:
  22. * const result = lastDayOfISOWeekYear(new Date(2005, 6, 2))
  23. * //=> Sun Jan 01 2006 00:00:00
  24. */
  25. export default function lastDayOfISOWeekYear(dirtyDate) {
  26. requiredArgs(1, arguments);
  27. var year = getISOWeekYear(dirtyDate);
  28. var fourthOfJanuary = new Date(0);
  29. fourthOfJanuary.setFullYear(year + 1, 0, 4);
  30. fourthOfJanuary.setHours(0, 0, 0, 0);
  31. var date = startOfISOWeek(fourthOfJanuary);
  32. date.setDate(date.getDate() - 1);
  33. return date;
  34. }