getISOWeeksInYear.js 1.4 KB

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