isSameWeek.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { normalizeDates } from "./_lib/normalizeDates.js";
  2. import { startOfWeek } from "./startOfWeek.js";
  3. /**
  4. * The {@link isSameWeek} function options.
  5. */
  6. /**
  7. * @name isSameWeek
  8. * @category Week Helpers
  9. * @summary Are the given dates in the same week (and month and year)?
  10. *
  11. * @description
  12. * Are the given dates in the same week (and month and year)?
  13. *
  14. * @param laterDate - The first date to check
  15. * @param earlierDate - The second date to check
  16. * @param options - An object with options
  17. *
  18. * @returns The dates are in the same week (and month and year)
  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 function isSameWeek(laterDate, earlierDate, options) {
  39. const [laterDate_, earlierDate_] = normalizeDates(
  40. options?.in,
  41. laterDate,
  42. earlierDate,
  43. );
  44. return (
  45. +startOfWeek(laterDate_, options) === +startOfWeek(earlierDate_, options)
  46. );
  47. }
  48. // Fallback for modularized imports:
  49. export default isSameWeek;