isSameDay.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { normalizeDates } from "./_lib/normalizeDates.js";
  2. import { startOfDay } from "./startOfDay.js";
  3. /**
  4. * The {@link isSameDay} function options.
  5. */
  6. /**
  7. * @name isSameDay
  8. * @category Day Helpers
  9. * @summary Are the given dates in the same day (and year and month)?
  10. *
  11. * @description
  12. * Are the given dates in the same day (and year and month)?
  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 day (and year and month)
  19. *
  20. * @example
  21. * // Are 4 September 06:00:00 and 4 September 18:00:00 in the same day?
  22. * const result = isSameDay(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 18, 0))
  23. * //=> true
  24. *
  25. * @example
  26. * // Are 4 September and 4 October in the same day?
  27. * const result = isSameDay(new Date(2014, 8, 4), new Date(2014, 9, 4))
  28. * //=> false
  29. *
  30. * @example
  31. * // Are 4 September, 2014 and 4 September, 2015 in the same day?
  32. * const result = isSameDay(new Date(2014, 8, 4), new Date(2015, 8, 4))
  33. * //=> false
  34. */
  35. export function isSameDay(laterDate, earlierDate, options) {
  36. const [dateLeft_, dateRight_] = normalizeDates(
  37. options?.in,
  38. laterDate,
  39. earlierDate,
  40. );
  41. return +startOfDay(dateLeft_) === +startOfDay(dateRight_);
  42. }
  43. // Fallback for modularized imports:
  44. export default isSameDay;