isSameSecond.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { startOfSecond } from "./startOfSecond.mjs";
  2. /**
  3. * @name isSameSecond
  4. * @category Second Helpers
  5. * @summary Are the given dates in the same second (and hour and day)?
  6. *
  7. * @description
  8. * Are the given dates in the same second (and hour and 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 second (and hour and day)
  16. *
  17. * @example
  18. * // Are 4 September 2014 06:30:15.000 and 4 September 2014 06:30.15.500 in the same second?
  19. * const result = isSameSecond(
  20. * new Date(2014, 8, 4, 6, 30, 15),
  21. * new Date(2014, 8, 4, 6, 30, 15, 500)
  22. * )
  23. * //=> true
  24. *
  25. * @example
  26. * // Are 4 September 2014 06:00:15.000 and 4 September 2014 06:01.15.000 in the same second?
  27. * const result = isSameSecond(
  28. * new Date(2014, 8, 4, 6, 0, 15),
  29. * new Date(2014, 8, 4, 6, 1, 15)
  30. * )
  31. * //=> false
  32. *
  33. * @example
  34. * // Are 4 September 2014 06:00:15.000 and 5 September 2014 06:00.15.000 in the same second?
  35. * const result = isSameSecond(
  36. * new Date(2014, 8, 4, 6, 0, 15),
  37. * new Date(2014, 8, 5, 6, 0, 15)
  38. * )
  39. * //=> false
  40. */
  41. export function isSameSecond(dateLeft, dateRight) {
  42. const dateLeftStartOfSecond = startOfSecond(dateLeft);
  43. const dateRightStartOfSecond = startOfSecond(dateRight);
  44. return +dateLeftStartOfSecond === +dateRightStartOfSecond;
  45. }
  46. // Fallback for modularized imports:
  47. export default isSameSecond;