getISOWeeksInYear.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { addWeeks } from "./addWeeks.js";
  2. import { millisecondsInWeek } from "./constants.js";
  3. import { startOfISOWeekYear } from "./startOfISOWeekYear.js";
  4. /**
  5. * The {@link getISOWeeksInYear} function options.
  6. */
  7. /**
  8. * @name getISOWeeksInYear
  9. * @category ISO Week-Numbering Year Helpers
  10. * @summary Get the number of weeks in an ISO week-numbering year of the given date.
  11. *
  12. * @description
  13. * Get the number of weeks in an ISO week-numbering year of the given date.
  14. *
  15. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  16. *
  17. * @param date - The given date
  18. * @param options - An object with options
  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. export function getISOWeeksInYear(date, options) {
  28. const thisYear = startOfISOWeekYear(date, options);
  29. const nextYear = startOfISOWeekYear(addWeeks(thisYear, 60));
  30. const diff = +nextYear - +thisYear;
  31. // Round the number of weeks to the nearest integer because the number of
  32. // milliseconds in a week is not constant (e.g. it's different in the week of
  33. // the daylight saving time clock shift).
  34. return Math.round(diff / millisecondsInWeek);
  35. }
  36. // Fallback for modularized imports:
  37. export default getISOWeeksInYear;