index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import isSameWeek from "../isSameWeek/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name isThisWeek
  5. * @category Week Helpers
  6. * @summary Is the given date in the same week as the current date?
  7. * @pure false
  8. *
  9. * @description
  10. * Is the given date in the same week as the current date?
  11. *
  12. * > ⚠️ Please note that this function is not present in the FP submodule as
  13. * > it uses `Date.now()` internally hence impure and can't be safely curried.
  14. *
  15. * ### v2.0.0 breaking changes:
  16. *
  17. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  18. *
  19. * @param {Date|Number} date - the date to check
  20. * @param {Object} [options] - the object with options
  21. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  22. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  23. * @returns {Boolean} the date is in this week
  24. * @throws {TypeError} 1 argument required
  25. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  26. *
  27. * @example
  28. * // If today is 25 September 2014, is 21 September 2014 in this week?
  29. * var result = isThisWeek(new Date(2014, 8, 21))
  30. * //=> true
  31. *
  32. * @example
  33. * // If today is 25 September 2014 and week starts with Monday
  34. * // is 21 September 2014 in this week?
  35. * var result = isThisWeek(new Date(2014, 8, 21), { weekStartsOn: 1 })
  36. * //=> false
  37. */
  38. export default function isThisWeek(dirtyDate, options) {
  39. requiredArgs(1, arguments);
  40. return isSameWeek(dirtyDate, Date.now(), options);
  41. }