SyntaxStudy
Sign Up
JavaScript Intermediate 4 min read

Comparing Dates

Comparing Dates

Compare dates using their numeric timestamps. The relational operators (<, >, <=, >=) work on dates because JavaScript coerces them to numbers.

Example
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
Pro Tip

To check date equality, compare getTime() values — never use === on Date objects (reference comparison).