getISOWeekYear.cjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. exports.getISOWeekYear = getISOWeekYear;
  3. var _index = require("./constructFrom.cjs");
  4. var _index2 = require("./startOfISOWeek.cjs");
  5. var _index3 = require("./toDate.cjs");
  6. /**
  7. * The {@link getISOWeekYear} function options.
  8. */
  9. /**
  10. * @name getISOWeekYear
  11. * @category ISO Week-Numbering Year Helpers
  12. * @summary Get the ISO week-numbering year of the given date.
  13. *
  14. * @description
  15. * Get the ISO week-numbering year of the given date,
  16. * which always starts 3 days before the year's first Thursday.
  17. *
  18. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  19. *
  20. * @param date - The given date
  21. *
  22. * @returns The ISO week-numbering year
  23. *
  24. * @example
  25. * // Which ISO-week numbering year is 2 January 2005?
  26. * const result = getISOWeekYear(new Date(2005, 0, 2))
  27. * //=> 2004
  28. */
  29. function getISOWeekYear(date, options) {
  30. const _date = (0, _index3.toDate)(date, options?.in);
  31. const year = _date.getFullYear();
  32. const fourthOfJanuaryOfNextYear = (0, _index.constructFrom)(_date, 0);
  33. fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4);
  34. fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0);
  35. const startOfNextYear = (0, _index2.startOfISOWeek)(
  36. fourthOfJanuaryOfNextYear,
  37. );
  38. const fourthOfJanuaryOfThisYear = (0, _index.constructFrom)(_date, 0);
  39. fourthOfJanuaryOfThisYear.setFullYear(year, 0, 4);
  40. fourthOfJanuaryOfThisYear.setHours(0, 0, 0, 0);
  41. const startOfThisYear = (0, _index2.startOfISOWeek)(
  42. fourthOfJanuaryOfThisYear,
  43. );
  44. if (_date.getTime() >= startOfNextYear.getTime()) {
  45. return year + 1;
  46. } else if (_date.getTime() >= startOfThisYear.getTime()) {
  47. return year;
  48. } else {
  49. return year - 1;
  50. }
  51. }