index.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  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. * @param {Date|Number} date - the given date
  17. * @returns {Number} the ISO week
  18. * @throws {TypeError} 1 argument required
  19. *
  20. * @example
  21. * // Which week of the ISO-week numbering year is 2 January 2005?
  22. * const result = getISOWeek(new Date(2005, 0, 2))
  23. * //=> 53
  24. */
  25. export default function getISOWeek(dirtyDate) {
  26. requiredArgs(1, arguments);
  27. var date = toDate(dirtyDate);
  28. var diff = startOfISOWeek(date).getTime() - startOfISOWeekYear(date).getTime();
  29. // Round the number of days 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) + 1;
  33. }