index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. import startOfISOWeekYear from "../startOfISOWeekYear/index.js";
  2. import addWeeks from "../addWeeks/index.js";
  3. import requiredArgs from "../_lib/requiredArgs/index.js";
  4. var MILLISECONDS_IN_WEEK = 604800000;
  5. /**
  6. * @name getISOWeeksInYear
  7. * @category ISO Week-Numbering Year Helpers
  8. * @summary Get the number of weeks in an ISO week-numbering year of the given date.
  9. *
  10. * @description
  11. * Get the number of weeks in an ISO week-numbering year of the given date.
  12. *
  13. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  14. *
  15. * @param {Date|Number} date - the given date
  16. * @returns {Number} the number of ISO weeks in a year
  17. * @throws {TypeError} 1 argument required
  18. *
  19. * @example
  20. * // How many weeks are in ISO week-numbering year 2015?
  21. * const result = getISOWeeksInYear(new Date(2015, 1, 11))
  22. * //=> 53
  23. */
  24. export default function getISOWeeksInYear(dirtyDate) {
  25. requiredArgs(1, arguments);
  26. var thisYear = startOfISOWeekYear(dirtyDate);
  27. var nextYear = startOfISOWeekYear(addWeeks(thisYear, 60));
  28. var diff = nextYear.valueOf() - thisYear.valueOf();
  29. // Round the number of weeks to the nearest integer
  30. // because the number of milliseconds in a week is not constant
  31. // (e.g. it's different in the week of the daylight saving time clock shift)
  32. return Math.round(diff / MILLISECONDS_IN_WEEK);
  33. }