The first date as a Date object or timestamp (number)
The second date as a Date object or timestamp (number)
True if both dates are in the same second, false otherwise or if either date is invalid
// Same second, different milliseconds
const result = isSameSecond(
new Date(2024, 5, 15, 14, 30, 45, 0),
new Date(2024, 5, 15, 14, 30, 45, 999)
);
// Returns: true
// Different seconds
const result2 = isSameSecond(
new Date(2024, 5, 15, 14, 30, 45, 999),
new Date(2024, 5, 15, 14, 30, 46, 0)
);
// Returns: false
// Same second, different milliseconds
const result3 = isSameSecond(
new Date(2024, 5, 15, 14, 30, 45, 123),
new Date(2024, 5, 15, 14, 30, 45, 456)
);
// Returns: true
// Same second of minute, different minutes
const result4 = isSameSecond(
new Date(2024, 5, 15, 14, 30, 45),
new Date(2024, 5, 15, 14, 31, 45)
);
// Returns: false (different minutes)
// Invalid dates return false
const result5 = isSameSecond(new Date("invalid"), new Date(2024, 5, 15, 14, 30, 45));
// Returns: false
Check if two dates are in the same second.
This function compares two dates and returns true if they fall within the same second, regardless of milliseconds.