JS Date Comparison - How to Compare Dates in JavaScript?
Last Updated :
11 Jul, 2025
There are various methods to compare dates in JavaScript.
Basic Date Comparisons
When comparing dates in JavaScript, it is important to understand that the Date objects represent points in time. This makes it easy to compare them using numerical values derived from the dates.
1. Comparing Dates Using getTime() Method
The getTime() method returns the number of milliseconds since the Unix epoch (January 1, 1970). This allows you to compare dates numerically.
JavaScript
const date1 = new Date('2024-08-29');
const date2 = new Date('2024-09-01');
if (date1.getTime() < date2.getTime()) {
console.log('date1 is earlier than date2');
} else if (date1.getTime() > date2.getTime()) {
console.log('date1 is later than date2');
} else {
console.log('date1 and date2 are the same');
}
Outputdate1 is earlier than date2
2. Comparing Dates Using Relational Operators
JavaScript allows you to use relational operators (<, <=, >, >=) directly on Date objects to compare dates based on their time values internally.
JavaScript
const date1 = new Date('2024-08-29');
const date2 = new Date('2024-09-01');
if (date1 < date2) {
console.log('date1 is earlier than date2');
} else if (date1 > date2) {
console.log('date1 is later than date2');
} else {
console.log('date1 and date2 are the same');
}
Outputdate1 is earlier than date2
Comparing Dates with Different Formats
Dates in JavaScript can be written in various formats, and comparing them requires parsing them into Date objects.
Parsing Date Strings
You can parse date strings using Date.parse() or new Date(). This converts date strings into Date objects that can be compared.
JavaScript
const dateStr1 = '2024-08-29';
const dateStr2 = 'August 29, 2024';
const date1 = new Date(dateStr1);
const date2 = new Date(dateStr2);
if (date1.getTime() === date2.getTime()) {
console.log('Dates are equal');
} else {
console.log('Dates are not equal');
}
How to Handle Time Zones in Date Comparisons?
Date comparisons can become complex when time zones are involved. By default, JavaScript dates are in local time unless specified otherwise. To handle comparisons consistently across time zones, you can use UTC methods like getUTCDate(), getUTCHours(), etc.
JavaScript
const date1 = new Date('2024-08-29T00:00:00Z');
const date2 = new Date('2024-08-29T00:00:00+02:00');
if (date1.getTime() === date2.getTime()) {
console.log('Dates are the same in UTC');
} else {
console.log('Dates are different');
}
OutputDates are different
How to Compare Only Date Parts (Ignoring Time)?
To compare only the date part (ignoring time), you can zero out the time portion by setting the hours, minutes, seconds, and milliseconds to zero.
JavaScript
const date1 = new Date('2024-08-29T10:00:00');
const date2 = new Date('2024-08-29T18:30:00');
// Zero out the time part
date1.setHours(0, 0, 0, 0);
date2.setHours(0, 0, 0, 0);
if (date1.getTime() === date2.getTime()) {
console.log('Dates are the same');
} else {
console.log('Dates are different');
}
Using Libraries for Date Comparisons
JavaScript provides built-in methods for date comparisons, libraries like Moment.js and date-fns offer additional functionality and convenience.
Using Moment.js
Moment.js provides isBefore(), isAfter(), and isSame() methods that make date comparisons.
JavaScript
const moment = require('moment');
const date1 = moment('2024-08-29');
const date2 = moment('2024-09-01');
console.log(date1.isBefore(date2));
console.log(date1.isSame(date2));
Output
true
false
Using date-fns
The date-fns is a modern JavaScript library that offers lightweight, modular functions for date manipulation and comparison.
JavaScript
const { isBefore, isEqual, parseISO } = require('date-fns');
const date1 = parseISO('2024-08-29');
const date2 = parseISO('2024-09-01');
console.log(isBefore(date1, date2));
console.log(isEqual(date1, date2));
Output
true
false
JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.
Explore
JavaScript Basics
Array & String
Function & Object
OOP
Asynchronous JavaScript
Exception Handling
DOM
Advanced Topics