getISOWeeksInYear.cjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. exports.getISOWeeksInYear = getISOWeeksInYear;
  3. var _index = require("./addWeeks.cjs");
  4. var _index2 = require("./constants.cjs");
  5. var _index3 = require("./startOfISOWeekYear.cjs");
  6. /**
  7. * The {@link getISOWeeksInYear} function options.
  8. */
  9. /**
  10. * @name getISOWeeksInYear
  11. * @category ISO Week-Numbering Year Helpers
  12. * @summary Get the number of weeks in an ISO week-numbering year of the given date.
  13. *
  14. * @description
  15. * Get the number of weeks in an ISO week-numbering year of the given date.
  16. *
  17. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  18. *
  19. * @param date - The given date
  20. * @param options - An object with options
  21. *
  22. * @returns The number of ISO weeks in a year
  23. *
  24. * @example
  25. * // How many weeks are in ISO week-numbering year 2015?
  26. * const result = getISOWeeksInYear(new Date(2015, 1, 11))
  27. * //=> 53
  28. */
  29. function getISOWeeksInYear(date, options) {
  30. const thisYear = (0, _index3.startOfISOWeekYear)(date, options);
  31. const nextYear = (0, _index3.startOfISOWeekYear)(
  32. (0, _index.addWeeks)(thisYear, 60),
  33. );
  34. const diff = +nextYear - +thisYear;
  35. // Round the number of weeks to the nearest integer because the number of
  36. // milliseconds in a week is not constant (e.g. it's different in the week of
  37. // the daylight saving time clock shift).
  38. return Math.round(diff / _index2.millisecondsInWeek);
  39. }