Compare Dates in JavaScript
Compare dates in JavaScript with getTime(), < and >, range checks, is-today, past or future checks and date-only checks. Explanation and output.
The sample output was captured with the clock set to 15 January 2024, 10:30:45 UTC. Your run will show your own date, time and time zone.
1// Method 1: Compare timestamps2function compareDates(date1, date2) {3 if (date1.getTime() === date2.getTime()) {4 return 0; // Equal5 } else if (date1.getTime() > date2.getTime()) {6 return 1; // date1 is later7 } else {8 return -1; // date1 is earlier9 }10}1112const date1 = new Date('2024-01-15');13const date2 = new Date('2024-01-20');14console.log('Compare:', compareDates(date1, date2));1516// Method 2: Simple comparisons17console.log('date1 > date2:', date1 > date2);18console.log('date1 < date2:', date1 < date2);19console.log('date1 === date2:', date1.getTime() === date2.getTime());2021// Method 3: Check if date is in range22function isDateInRange(date, startDate, endDate) {23 return date >= startDate && date <= endDate;24}2526const checkDate = new Date('2024-01-18');27const start = new Date('2024-01-15');28const end = new Date('2024-01-20');29console.log('In range:', isDateInRange(checkDate, start, end));3031// Method 4: Check if date is today32function isToday(date) {33 const today = new Date();34 return date.getDate() === today.getDate() &&35 date.getMonth() === today.getMonth() &&36 date.getFullYear() === today.getFullYear();37}3839console.log('Is today:', isToday(new Date()));4041// Method 5: Check if date is past/future42function isPast(date) {43 return date < new Date();44}4546function isFuture(date) {47 return date > new Date();48}4950const oneDay = 24 * 60 * 60 * 1000;51console.log('Is past:', isPast(new Date(Date.now() - oneDay))); // yesterday52console.log('Is future:', isFuture(new Date(Date.now() + oneDay))); // tomorrow5354// Method 6: Compare dates (ignore time)55function compareDatesOnly(date1, date2) {56 const d1 = new Date(date1.getFullYear(), date1.getMonth(), date1.getDate());57 const d2 = new Date(date2.getFullYear(), date2.getMonth(), date2.getDate());58 return d1.getTime() - d2.getTime();59}6061const dateWithTime = new Date('2024-01-15T15:30:00');62const dateWithoutTime = new Date('2024-01-15T00:00:00');63console.log('Same date (ignore time):', compareDatesOnly(dateWithTime, dateWithoutTime) === 0);
Sample Output
date1 === date2: false
This line compares getTime() values. They differ because the dates are five days apart.
In this one
- Why getTime() is the safe way to compare
- Why === fails on Date objects
- How to check ranges, today, past and future
- How to compare dates while ignoring time
How it works
- 1
compareDates returns -1 because 15 January is earlier.
- 2
< and > compare correctly, and getTime() checks equality.
- 3
isDateInRange accepts 18 January.
- 4
isToday checks the current date.
- 5
isPast and isFuture test dates a day before and after now.
- 6
compareDatesOnly ignores the clock time.
Edge cases
Using === on dates
new Date(0) === new Date(0) is false.
Compare getTime() values instead.
Fixed years in tests
isFuture(new Date("2025-01-01")) is false after 2025.
Build test dates relative to Date.now().
Date-only strings and time zones
new Date("2024-01-15") is midnight UTC, which can be 14 January in the US.
Parse with explicit times or use new Date(y, m, d) for local dates.
What's going on
Comparing dates answers questions like: is the assignment late, is the exam today, or is a date inside the admission window?
Method 1 compares timestamps. getTime() returns milliseconds since 1970, a plain number, so comparing two numbers is reliable. The function returns 0 if equal, 1 if the first date is later, and -1 if it is earlier. That shape is the same one sort() expects.
Method 2 uses < and > directly. They work because JavaScript converts dates to numbers for those operators. But === does not work for equality. Two Date objects are different objects, so date1 === date2 is false even when they hold the same time. Use date1.getTime() === date2.getTime() for equality.
Method 3 checks a range: start <= date && date <= end. 18 January is between 15 and 20 January, so it returns true.
Method 4 checks if a date is today. It compares day of month, month and year. This ignores the time part, so 9 AM and 9 PM today both count.
Method 5 checks past and future by comparing with new Date(). The examples use dates relative to now, a day ago and a day ahead. That way the result stays correct whenever you run it. The first version used fixed years, so "is future" quietly became false once that year passed.
Method 6 compares only the date part. It builds new dates at midnight from year, month and day, then subtracts. 15 January at 3:30 PM and 15 January at midnight count as the same date.
Time zones can make two dates fall on different days. Compare in one time zone, and use helper functions so the rules live in one place.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does new Date(5) === new Date(5) return?
Pick an answer to check it.
Question 2
compareDates(later, earlier) returns?
Pick an answer to check it.
Questions people ask
Can I sort an array of dates?
Yes: dates.sort((a, b) => a - b).
Does <= work on dates?
Yes. Only == and === compare object identity.
Why build a new Date for date-only checks?
It throws away hours and minutes so only the calendar day remains.