isSameHour.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { normalizeDates } from "./_lib/normalizeDates.js";
  2. import { startOfHour } from "./startOfHour.js";
  3. /**
  4. * The {@link isSameHour} function options.
  5. */
  6. /**
  7. * @name isSameHour
  8. * @category Hour Helpers
  9. * @summary Are the given dates in the same hour (and same day)?
  10. *
  11. * @description
  12. * Are the given dates in the same hour (and same day)?
  13. *
  14. * @param dateLeft - The first date to check
  15. * @param dateRight - The second date to check
  16. * @param options - An object with options
  17. *
  18. * @returns The dates are in the same hour (and same day)
  19. *
  20. * @example
  21. * // Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour?
  22. * const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 6, 30))
  23. * //=> true
  24. *
  25. * @example
  26. * // Are 4 September 2014 06:00:00 and 5 September 06:00:00 in the same hour?
  27. * const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 5, 6, 0))
  28. * //=> false
  29. */
  30. export function isSameHour(dateLeft, dateRight, options) {
  31. const [dateLeft_, dateRight_] = normalizeDates(
  32. options?.in,
  33. dateLeft,
  34. dateRight,
  35. );
  36. return +startOfHour(dateLeft_) === +startOfHour(dateRight_);
  37. }
  38. // Fallback for modularized imports:
  39. export default isSameHour;