index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import toDate from "../toDate/index.js";
  2. import startOfISOWeek from "../startOfISOWeek/index.js";
  3. import startOfISOWeekYear from "../startOfISOWeekYear/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. var MILLISECONDS_IN_WEEK = 604800000;
  6. /**
  7. * @name getISOWeek
  8. * @category ISO Week Helpers
  9. * @summary Get the ISO week of the given date.
  10. *
  11. * @description
  12. * Get the ISO week of the given date.
  13. *
  14. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  15. *
  16. * ### v2.0.0 breaking changes:
  17. *
  18. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  19. *
  20. * @param {Date|Number} date - the given date
  21. * @returns {Number} the ISO week
  22. * @throws {TypeError} 1 argument required
  23. *
  24. * @example
  25. * // Which week of the ISO-week numbering year is 2 January 2005?
  26. * const result = getISOWeek(new Date(2005, 0, 2))
  27. * //=> 53
  28. */
  29. export default function getISOWeek(dirtyDate) {
  30. requiredArgs(1, arguments);
  31. var date = toDate(dirtyDate);
  32. var diff = startOfISOWeek(date).getTime() - startOfISOWeekYear(date).getTime(); // Round the number of days 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) + 1;
  36. }