isSameWeek.mjs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { startOfWeek } from "./startOfWeek.mjs";
  2. /**
  3. * The {@link isSameWeek} function options.
  4. */
  5. /**
  6. * @name isSameWeek
  7. * @category Week Helpers
  8. * @summary Are the given dates in the same week (and month and year)?
  9. *
  10. * @description
  11. * Are the given dates in the same week (and month and year)?
  12. *
  13. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  14. *
  15. * @param dateLeft - The first date to check
  16. * @param dateRight - The second date to check
  17. * @param options - An object with options
  18. *
  19. * @returns The dates are in the same week (and month and year)
  20. *
  21. * @example
  22. * // Are 31 August 2014 and 4 September 2014 in the same week?
  23. * const result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4))
  24. * //=> true
  25. *
  26. * @example
  27. * // If week starts with Monday,
  28. * // are 31 August 2014 and 4 September 2014 in the same week?
  29. * const result = isSameWeek(new Date(2014, 7, 31), new Date(2014, 8, 4), {
  30. * weekStartsOn: 1
  31. * })
  32. * //=> false
  33. *
  34. * @example
  35. * // Are 1 January 2014 and 1 January 2015 in the same week?
  36. * const result = isSameWeek(new Date(2014, 0, 1), new Date(2015, 0, 1))
  37. * //=> false
  38. */
  39. export function isSameWeek(dateLeft, dateRight, options) {
  40. const dateLeftStartOfWeek = startOfWeek(dateLeft, options);
  41. const dateRightStartOfWeek = startOfWeek(dateRight, options);
  42. return +dateLeftStartOfWeek === +dateRightStartOfWeek;
  43. }
  44. // Fallback for modularized imports:
  45. export default isSameWeek;