isSameHour.js 1.2 KB

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