Comparing Dates
Compare dates using their numeric timestamps. The relational operators (<, >, <=, >=) work on dates because JavaScript coerces them to numbers.
Compare dates using their numeric timestamps. The relational operators (<, >, <=, >=) work on dates because JavaScript coerces them to numbers.
const a = new Date("2024-06-01");
const b = new Date("2024-12-31");
a < b // true
a > b // false
// Equality requires .getTime()
a === b // false (different objects)
a.getTime() === b.getTime() // true if same moment
// Check if a date is in the past
function isPast(date) {
return date.getTime() < Date.now();
}
// Sort array of dates
dates.sort((a, b) => a - b); // ascending
To check date equality, compare getTime() values — never use === on Date objects (reference comparison).
More in JavaScript