index.js 1.7 KB

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