index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isSameWeek;
  6. var _index = _interopRequireDefault(require("../startOfWeek/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name isSameWeek
  11. * @category Week Helpers
  12. * @summary Are the given dates in the same week?
  13. *
  14. * @description
  15. * Are the given dates in the same week?
  16. *
  17. * ### v2.0.0 breaking changes:
  18. *
  19. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  20. *
  21. * @param {Date|Number} dateLeft - the first date to check
  22. * @param {Date|Number} dateRight - the second date to check
  23. * @param {Object} [options] - an object with options.
  24. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  25. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  26. * @returns {Boolean} the dates are in the same week
  27. * @throws {TypeError} 2 arguments required
  28. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  29. *
  30. * @example
  31. * // Are 31 August 2014 and 4 September 2014 in the same week?
  32. * var result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4))
  33. * //=> true
  34. *
  35. * @example
  36. * // If week starts with Monday,
  37. * // are 31 August 2014 and 4 September 2014 in the same week?
  38. * var result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4), {
  39. * weekStartsOn: 1
  40. * })
  41. * //=> false
  42. */
  43. function isSameWeek(dirtyDateLeft, dirtyDateRight, dirtyOptions) {
  44. (0, _index2.default)(2, arguments);
  45. var dateLeftStartOfWeek = (0, _index.default)(dirtyDateLeft, dirtyOptions);
  46. var dateRightStartOfWeek = (0, _index.default)(dirtyDateRight, dirtyOptions);
  47. return dateLeftStartOfWeek.getTime() === dateRightStartOfWeek.getTime();
  48. }
  49. module.exports = exports.default;