index.js 2.0 KB

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