getISOWeek.cjs 1.2 KB

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