lastDayOfISOWeekYear.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. exports.lastDayOfISOWeekYear = lastDayOfISOWeekYear;
  3. var _index = require("./getISOWeekYear.js");
  4. var _index2 = require("./startOfISOWeek.js");
  5. var _index3 = require("./constructFrom.js");
  6. /**
  7. * @name lastDayOfISOWeekYear
  8. * @category ISO Week-Numbering Year Helpers
  9. * @summary Return the last day of an ISO week-numbering year for the given date.
  10. *
  11. * @description
  12. * Return the last day of an ISO week-numbering year,
  13. * which always starts 3 days before the year's first Thursday.
  14. * The result will be in the local timezone.
  15. *
  16. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  17. *
  18. * @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).
  19. *
  20. * @param date - The original date
  21. *
  22. * @returns The end of an ISO week-numbering year
  23. *
  24. * @example
  25. * // The last day of an ISO week-numbering year for 2 July 2005:
  26. * const result = lastDayOfISOWeekYear(new Date(2005, 6, 2))
  27. * //=> Sun Jan 01 2006 00:00:00
  28. */
  29. function lastDayOfISOWeekYear(date) {
  30. const year = (0, _index.getISOWeekYear)(date);
  31. const fourthOfJanuary = (0, _index3.constructFrom)(date, 0);
  32. fourthOfJanuary.setFullYear(year + 1, 0, 4);
  33. fourthOfJanuary.setHours(0, 0, 0, 0);
  34. const _date = (0, _index2.startOfISOWeek)(fourthOfJanuary);
  35. _date.setDate(_date.getDate() - 1);
  36. return _date;
  37. }