isSameHour.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import { startOfHour } from "./startOfHour.mjs";
  2. /**
  3. * @name isSameHour
  4. * @category Hour Helpers
  5. * @summary Are the given dates in the same hour (and same day)?
  6. *
  7. * @description
  8. * Are the given dates in the same hour (and same day)?
  9. *
  10. * @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).
  11. *
  12. * @param dateLeft - The first date to check
  13. * @param dateRight - The second date to check
  14. *
  15. * @returns The dates are in the same hour (and same day)
  16. *
  17. * @example
  18. * // Are 4 September 2014 06:00:00 and 4 September 06:30:00 in the same hour?
  19. * const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 4, 6, 30))
  20. * //=> true
  21. *
  22. * @example
  23. * // Are 4 September 2014 06:00:00 and 5 September 06:00:00 in the same hour?
  24. * const result = isSameHour(new Date(2014, 8, 4, 6, 0), new Date(2014, 8, 5, 6, 0))
  25. * //=> false
  26. */
  27. export function isSameHour(dateLeft, dateRight) {
  28. const dateLeftStartOfHour = startOfHour(dateLeft);
  29. const dateRightStartOfHour = startOfHour(dateRight);
  30. return +dateLeftStartOfHour === +dateRightStartOfHour;
  31. }
  32. // Fallback for modularized imports:
  33. export default isSameHour;