index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getWeekOfMonth;
  6. var _index = _interopRequireDefault(require("../getDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../getDay/index.js"));
  8. var _index3 = _interopRequireDefault(require("../startOfMonth/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  10. var _index5 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. /**
  13. * @name getWeekOfMonth
  14. * @category Week Helpers
  15. * @summary Get the week of the month of the given date.
  16. *
  17. * @description
  18. * Get the week of the month of the given date.
  19. *
  20. * ### v2.0.0 breaking changes:
  21. *
  22. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  23. *
  24. * @param {Date|Number} date - the given date
  25. * @param {Object} [options] - an object with options.
  26. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  27. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  28. * @returns {Number} the week of month
  29. * @throws {TypeError} 1 argument required
  30. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  31. *
  32. * @example
  33. * // Which week of the month is 9 November 2017?
  34. * var result = getWeekOfMonth(new Date(2017, 10, 9))
  35. * //=> 2
  36. */
  37. function getWeekOfMonth(date, dirtyOptions) {
  38. (0, _index5.default)(1, arguments);
  39. var options = dirtyOptions || {};
  40. var locale = options.locale;
  41. var localeWeekStartsOn = locale && locale.options && locale.options.weekStartsOn;
  42. var defaultWeekStartsOn = localeWeekStartsOn == null ? 0 : (0, _index4.default)(localeWeekStartsOn);
  43. var weekStartsOn = options.weekStartsOn == null ? defaultWeekStartsOn : (0, _index4.default)(options.weekStartsOn); // Test if weekStartsOn is between 0 and 6 _and_ is not NaN
  44. if (!(weekStartsOn >= 0 && weekStartsOn <= 6)) {
  45. throw new RangeError('weekStartsOn must be between 0 and 6 inclusively');
  46. }
  47. var currentDayOfMonth = (0, _index.default)(date);
  48. if (isNaN(currentDayOfMonth)) {
  49. return currentDayOfMonth;
  50. }
  51. var startWeekDay = (0, _index2.default)((0, _index3.default)(date));
  52. var lastDayOfFirstWeek = 0;
  53. if (startWeekDay >= weekStartsOn) {
  54. lastDayOfFirstWeek = weekStartsOn + 7 - startWeekDay;
  55. } else {
  56. lastDayOfFirstWeek = weekStartsOn - startWeekDay;
  57. }
  58. var weekNumber = 1;
  59. if (currentDayOfMonth > lastDayOfFirstWeek) {
  60. var remainingDaysAfterFirstWeek = currentDayOfMonth - lastDayOfFirstWeek;
  61. weekNumber = weekNumber + Math.ceil(remainingDaysAfterFirstWeek / 7);
  62. }
  63. return weekNumber;
  64. }
  65. module.exports = exports.default;