isSameSecond.js 1.6 KB

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