index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
  2. import getDate from "../getDate/index.js";
  3. import getDay from "../getDay/index.js";
  4. import startOfMonth from "../startOfMonth/index.js";
  5. import requiredArgs from "../_lib/requiredArgs/index.js";
  6. import toInteger from "../_lib/toInteger/index.js";
  7. /**
  8. * @name getWeekOfMonth
  9. * @category Week Helpers
  10. * @summary Get the week of the month of the given date.
  11. *
  12. * @description
  13. * Get the week of the month of the given date.
  14. *
  15. * @param {Date|Number} date - the given date
  16. * @param {Object} [options] - an object with options.
  17. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  18. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  19. * @returns {Number} the week of month
  20. * @throws {TypeError} 1 argument required
  21. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6 inclusively
  22. *
  23. * @example
  24. * // Which week of the month is 9 November 2017?
  25. * const result = getWeekOfMonth(new Date(2017, 10, 9))
  26. * //=> 2
  27. */
  28. export default function getWeekOfMonth(date, options) {
  29. var _ref, _ref2, _ref3, _options$weekStartsOn, _options$locale, _options$locale$optio, _defaultOptions$local, _defaultOptions$local2;
  30. requiredArgs(1, arguments);
  31. var defaultOptions = getDefaultOptions();
  32. var weekStartsOn = toInteger((_ref = (_ref2 = (_ref3 = (_options$weekStartsOn = options === null || options === void 0 ? void 0 : options.weekStartsOn) !== null && _options$weekStartsOn !== void 0 ? _options$weekStartsOn : options === null || options === void 0 ? void 0 : (_options$locale = options.locale) === null || _options$locale === void 0 ? void 0 : (_options$locale$optio = _options$locale.options) === null || _options$locale$optio === void 0 ? void 0 : _options$locale$optio.weekStartsOn) !== null && _ref3 !== void 0 ? _ref3 : defaultOptions.weekStartsOn) !== null && _ref2 !== void 0 ? _ref2 : (_defaultOptions$local = defaultOptions.locale) === null || _defaultOptions$local === void 0 ? void 0 : (_defaultOptions$local2 = _defaultOptions$local.options) === null || _defaultOptions$local2 === void 0 ? void 0 : _defaultOptions$local2.weekStartsOn) !== null && _ref !== void 0 ? _ref : 0);
  33. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  34. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  35. }
  36. var currentDayOfMonth = getDate(date);
  37. if (isNaN(currentDayOfMonth)) return NaN;
  38. var startWeekDay = getDay(startOfMonth(date));
  39. var lastDayOfFirstWeek = weekStartsOn - startWeekDay;
  40. if (lastDayOfFirstWeek <= 0) lastDayOfFirstWeek += 7;
  41. var remainingDaysAfterFirstWeek = currentDayOfMonth - lastDayOfFirstWeek;
  42. return Math.ceil(remainingDaysAfterFirstWeek / 7) + 1;
  43. }