getISOWeek.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. "use strict";
  2. exports.getISOWeek = getISOWeek;
  3. var _index = require("./constants.js");
  4. var _index2 = require("./startOfISOWeek.js");
  5. var _index3 = require("./startOfISOWeekYear.js");
  6. var _index4 = require("./toDate.js");
  7. /**
  8. * @name getISOWeek
  9. * @category ISO Week Helpers
  10. * @summary Get the ISO week of the given date.
  11. *
  12. * @description
  13. * Get the ISO week of the given date.
  14. *
  15. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  16. *
  17. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  18. *
  19. * @param date - The given date
  20. *
  21. * @returns The ISO week
  22. *
  23. * @example
  24. * // Which week of the ISO-week numbering year is 2 January 2005?
  25. * const result = getISOWeek(new Date(2005, 0, 2))
  26. * //=> 53
  27. */
  28. function getISOWeek(date) {
  29. const _date = (0, _index4.toDate)(date);
  30. const diff =
  31. +(0, _index2.startOfISOWeek)(_date) -
  32. +(0, _index3.startOfISOWeekYear)(_date);
  33. // Round the number of weeks to the nearest integer because the number of
  34. // milliseconds in a week is not constant (e.g. it's different in the week of
  35. // the daylight saving time clock shift).
  36. return Math.round(diff / _index.millisecondsInWeek) + 1;
  37. }