Date comparison: JavaScript uses comparison operators (<, >, =, <>) with Date objects to compare chronological order
Note: JavaScript compares dates by converting them to timestamps (milliseconds since epoch) internally.
- Create Date objects for the dates to compare
- Use comparison operators to evaluate relationships
- Understand that earlier dates have smaller timestamp values
const date2 = new Date('2023-01-20');
date1 < date2
const date1 = new Date('2023-01-15'); const date2 = new Date('2023-01-20');
date1 < date2 evaluates if January 15 comes before January 20
Since January 15 occurs before January 20, the result is true
date1 < date2 = true
• Date ordering: Earlier dates have smaller timestamp values
• Object comparison: JavaScript converts Date objects to timestamps for comparison
• Comparison operators: Same as for numbers but applied chronologically
• Practice Tip: Think of dates as positions on a timeline: left (earlier) < right (later)
- new Date('2023-02-01') > new Date('2023-01-31') - February comes after January
- new Date('2023-03-10') === new Date('2023-03-10') - Same dates are equal
- new Date('2022-12-31') < new Date('2023-01-01') - End of year before next year
- JavaScript automatically handles leap years and month lengths in date comparisons
- Always use Date objects for reliable comparison
- Chronological thinking: earlier dates have "smaller" timestamp values than later dates
Q: Can I compare date strings directly?
A: It's better to use Date objects. String comparison follows alphabetical order, not chronological.
Q: What about time zones?
A: Date objects store UTC time internally, ensuring consistent comparisons regardless of local time zone.
Greater than operator: > returns true when the left date occurs after the right date chronologically
Note: This operator compares timestamps: later dates have higher timestamp values than earlier dates.
- Create Date objects for comparison
- Use the > operator to find dates occurring later
- Understand that higher timestamps correspond to later dates
const refDate = new Date('2023-06-01');
date > refDate
const date = new Date('2023-06-15'); const refDate = new Date('2023-06-01');
date > refDate finds if June 15 occurs after June 1
Since June 15 is after June 1, the result is true
This comparison returns true for dates after June 1, 2023
• Timestamp comparison: Later dates have higher timestamp values
• Chronological order: Higher timestamps mean later dates
• Practical use: Commonly used for finding recent records or future events
• Practice Tip: Remember: dates further right on timeline have higher values
- new Date('2023-12-26') > new Date('2023-12-25') - Day after Christmas
- new Date('2023-03-01') > new Date('2023-02-28') - March after February
- new Date('2024-01-01') > new Date('2023-12-31') - New year after previous
- Use greater than for "after" conditions: date > new Date('2000-01-01') means after year 2000
- Combine with other operators for ranges: date > start && date < end
- Think of timeline: dates to the right have higher timestamp values
Q: What if I want dates on or after a specific date?
A: Use the greater than or equal operator: date >= new Date('2023-06-01')
Q: Can I use this with time components?
A: Yes, datetime comparisons work: new Date('2023-06-01T10:30') > new Date('2023-06-01T09:00')
Less than operator: < returns true when the left date occurs before the right date chronologically
Note: This operator compares timestamps: earlier dates have lower timestamp values than later dates.
- Create Date objects for comparison
- Use the < operator to find dates occurring earlier
- Understand that lower timestamps correspond to earlier dates
const refDate = new Date('2023-06-01');
date < refDate
const date = new Date('2023-05-15'); const refDate = new Date('2023-06-01');
date < refDate finds if May 15 occurs before June 1
Since May 15 is before June 1, the result is true
This comparison returns true for dates before June 1, 2023
• Timestamp comparison: Earlier dates have lower timestamp values
• Chronological order: Lower timestamps mean earlier dates
• Practical use: Commonly used for finding old records or past events
• Practice Tip: Remember: dates further left on timeline have lower values
- new Date('2022-12-31') < new Date('2023-01-01') - Last day of previous year
- new Date('2023-02-14') < new Date('2023-02-28') - Valentine's before end of Feb
- new Date('2023-01-01') < new Date('2023-12-31') - New Year before end of year
- Use less than for "before" conditions: date < new Date('2010-01-01') means before 2010
- Combine with other operators for ranges: date > start && date < end
- Think of timeline: dates to the left have lower timestamp values
Q: What if I want dates on or before a specific date?
A: Use the less than or equal operator: date <= new Date('2023-06-01')
Q: Can I use this with time components?
A: Yes, datetime comparisons work: new Date('2023-06-01T08:00') < new Date('2023-06-01T09:30')
Strict equality operator: === returns true only when both Date objects have identical timestamps
Note: For Date objects, use getTime() to compare timestamps, as === compares references, not values.
- Create Date objects for comparison
- Use getTime() method to compare timestamps
- Structure comparison to return boolean result
const date2 = new Date('2023-06-01');
date1.getTime() === date2.getTime()
const date1 = new Date('2023-06-01'); const date2 = new Date('2023-06-01');
date1.getTime() === date2.getTime() compares actual date values
Since both dates are identical, the result is true
This comparison returns true only for identical dates
• Value comparison: Use getTime() to compare actual date values
• Reference vs value: === compares references, not values for objects
• Practical use: Commonly used for finding specific events or appointments
• Practice Tip: Remember: date1 === date2 rarely true for different objects
- new Date('2023-01-01').getTime() === new Date('2023-01-01').getTime() - New Year's Day
- new Date('2023-12-25').getTime() === new Date('2023-12-25').getTime() - Christmas Day
- new Date('2023-07-04').getTime() === new Date('2023-07-04').getTime() - Independence Day
- Use getTime() for actual date value comparison, not ===
- Combine with other conditions: date.getTime() === target.getTime() && status === 'active'
- Date objects with same date/time have same timestamp value
Q: Why doesn't date1 === date2 work for Date objects?
A: Because === compares object references, not values. Two different Date objects are different references even if they represent the same date.
Q: What about datetime values?
A: Same principle: use getTime() to compare the actual timestamp values.
Not equal operator: !== or != returns true when dates have different timestamps
Note: Use getTime() to properly compare date values for inequality.
- Create Date objects for comparison
- Use getTime() to compare timestamps
- Apply !== operator to find non-matching dates
const refDate = new Date('2023-06-01');
date.getTime() !== refDate.getTime()
const date = new Date('2023-06-15'); const refDate = new Date('2023-06-01');
date.getTime() !== refDate.getTime() checks if timestamps are different
Since June 15 and June 1 are different dates, the result is true
This comparison returns true for dates different from June 1, 2023
• Value comparison: Use getTime() to compare actual date values
• Exclusion: Only different timestamps return true
• Practical use: Commonly used for excluding specific dates
• Practice Tip: Remember: !== and != work similarly with Date objects
- new Date('2023-01-02').getTime() !== new Date('2023-01-01').getTime() - Jan 2 vs Jan 1
- new Date('2023-12-24').getTime() !== new Date('2023-12-25').getTime() - Christmas Eve vs Day
- new Date('2023-07-05').getTime() !== new Date('2023-07-04').getTime() - Day after Independence
- Use getTime() for accurate value comparison: date.getTime() !== target.getTime()
- Combine with other operators: date.getTime() !== holiday.getTime() && date.getTime() < end.getTime()
- Both !== and != work with getTime() for value comparison
Q: What's the difference between !== and != with dates?
A: Both work the same way when using getTime(), but !== is preferred for strict comparison.
Q: How does undefined/null affect comparisons?
A: null.getTime() will cause an error. Always validate date objects before comparison.