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 (and month and year)?
  7. *
  8. * @description
  9. * Are the given dates in the same week (and month and year)?
  10. *
  11. * @param {Date|Number} dateLeft - the first date to check
  12. * @param {Date|Number} dateRight - the second date to check
  13. * @param {Object} [options] - an object with options.
  14. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  15. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  16. * @returns {Boolean} the dates are in the same week (and month and year)
  17. * @throws {TypeError} 2 arguments required
  18. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  19. *
  20. * @example
  21. * // Are 31 August 2014 and 4 September 2014 in the same week?
  22. * const result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4))
  23. * //=> true
  24. *
  25. * @example
  26. * // If week starts with Monday,
  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. * weekStartsOn: 1
  30. * })
  31. * //=> false
  32. *
  33. * @example
  34. * // Are 1 January 2014 and 1 January 2015 in the same week?
  35. * const result = isSameWeek(new Date(2014, 0, 1), new Date(2015, 0, 1))
  36. * //=> false
  37. */
  38. export default function isSameWeek(dirtyDateLeft, dirtyDateRight, options) {
  39. requiredArgs(2, arguments);
  40. var dateLeftStartOfWeek = startOfWeek(dirtyDateLeft, options);
  41. var dateRightStartOfWeek = startOfWeek(dirtyDateRight, options);
  42. return dateLeftStartOfWeek.getTime() === dateRightStartOfWeek.getTime();
  43. }