index.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. * ### v2.0.0 breaking changes:
  16. *
  17. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  18. *
  19. * @param {Date|Number} date - the given date
  20. * @returns {Number} the number of ISO weeks in a year
  21. * @throws {TypeError} 1 argument required
  22. *
  23. * @example
  24. * // How many weeks are in ISO week-numbering year 2015?
  25. * const result = getISOWeeksInYear(new Date(2015, 1, 11))
  26. * //=> 53
  27. */
  28. export default function getISOWeeksInYear(dirtyDate) {
  29. requiredArgs(1, arguments);
  30. var thisYear = startOfISOWeekYear(dirtyDate);
  31. var nextYear = startOfISOWeekYear(addWeeks(thisYear, 60));
  32. var diff = nextYear.valueOf() - thisYear.valueOf(); // Round the number of weeks to the nearest integer
  33. // because the number of milliseconds in a week is not constant
  34. // (e.g. it's different in the week of the daylight saving time clock shift)
  35. return Math.round(diff / MILLISECONDS_IN_WEEK);
  36. }