The Date object will do what you want - construct one for each date, then compare them using the >, <, <= or >=.

The ==, !=, ===, and !== operators require you to use date.getTime() as in

var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();

to be clear just checking for equality directly with the date objects won't work

var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2);   // prints false (wrong!) 
console.log(d1 === d2);  // prints false (wrong!)
console.log(d1 != d2);   // prints true  (wrong!)
console.log(d1 !== d2);  // prints true  (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.


For the curious, date.getTime() documentation:

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.)

Answer from moonshadow on Stack Overflow
Top answer
1 of 16
3411

The Date object will do what you want - construct one for each date, then compare them using the >, <, <= or >=.

The ==, !=, ===, and !== operators require you to use date.getTime() as in

var d1 = new Date();
var d2 = new Date(d1);
var same = d1.getTime() === d2.getTime();
var notSame = d1.getTime() !== d2.getTime();

to be clear just checking for equality directly with the date objects won't work

var d1 = new Date();
var d2 = new Date(d1);

console.log(d1 == d2);   // prints false (wrong!) 
console.log(d1 === d2);  // prints false (wrong!)
console.log(d1 != d2);   // prints true  (wrong!)
console.log(d1 !== d2);  // prints true  (wrong!)
console.log(d1.getTime() === d2.getTime()); // prints true (correct)

I suggest you use drop-downs or some similar constrained form of date entry rather than text boxes, though, lest you find yourself in input validation hell.


For the curious, date.getTime() documentation:

Returns the numeric value of the specified date as the number of milliseconds since January 1, 1970, 00:00:00 UTC. (Negative values are returned for prior times.)

2 of 16
575

Compare < and > just as usual, but anything involving == or === should use a + prefix. Like so:

const x = new Date('2013-05-23');
const y = new Date('2013-05-23');

// less than, greater than is fine:
console.log('x < y', x < y); // false
console.log('x > y', x > y); // false
console.log('x <= y', x <= y); // true
console.log('x >= y', x >= y); // true
console.log('x === y', x === y); // false, oops!

// anything involving '==' or '===' should use the '+' prefix
// it will then compare the dates' millisecond values

console.log('+x === +y', +x === +y); // true

🌐
freeCodeCamp
freecodecamp.org › news › javascript-date-comparison-how-to-compare-dates-in-js
JavaScript Date Comparison – How to Compare Dates in JS
November 7, 2024 - In this article, we will learn how to perform date comparisons in JavaScript. If you need the code quickly, here it is: const compareDates = (d1, d2) => { let date1 = new Date(d1).getTime(); let date2 = new Date(d2).getTime(); if (date1 < date2) { console.log(`${d1} is less than ${d2}`); } else if (date1 > date2) { console.log(`${d1} is greater than ${d2}`); } else { console.log(`Both dates are equal`); } }; compareDates("06/21/2022", "07/28/2021"); compareDates("01/01/2001", "01/01/2001"); compareDates("11/01/2021", "02/01/2022"); This will return: "06/21/2022 is greater than 07/28/2021" "Both dates are equal" "11/01/2021 is less than 02/01/2022" When we think of date comparison in JavaScript, we think of using the Date object (Date()) and, of course, it works.
Discussions

Comparing two dates in JavaScript is easier than you may think.
FYI, the month you set is June not May because it’s 0 based. More on reddit.com
🌐 r/learnjavascript
15
206
May 11, 2022
How to compare date with another in typescript?
I think you can use the getTime method of your Date object instances to compare them. Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTime Minimal example: const incoming = new Date(/* date you are receiving */) const deadline = new Date() deadline.setHours(deadline.getHours() + 2) if (incoming.getTime() > deadline.getTime()) { // Error... } // Everything is fine, do your work More on reddit.com
🌐 r/node
16
7
July 1, 2022
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-date-exercise-5.php
JavaScript: Comparison between two dates - w3resource
July 17, 2025 - var compare_dates = function(date1,date2){ if (date1>date2) return ("Date1 > Date2"); else if (date1<date2) return ("Date2 > Date1"); else return ("Date1 = Date2"); } console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:00'))); console.log(compare_dates(new Date('11/14/2013 00:01'), new Date('11/14/2013 00:00'))); console.log(compare_dates(new Date('11/14/2013 00:00'), new Date('11/14/2013 00:01')));
🌐
InfluxData
influxdata.com › home › comparing two dates in javascript: a guide
Comparing Two Dates in JavaScript: A Guide | InfluxData
June 26, 2024 - You’ve just seen that using the ... Can you evaluate whether a Date comes first or before another one, using the greater-than (>) and lesser-than ......
🌐
C# Corner
c-sharpcorner.com › UploadFile › 8911c4 › how-to-compare-two-dates-using-javascript
How to Compare Two Dates In JavaScript
March 15, 2023 - <script type="text/javascript"language="javascript"> function CompareDate() { //Note: 00 is month i.e. January var dateOne = new Date(2010, 00, 15); //Year, Month, Date var dateTwo = new Date(2011, 00, 15); //Year, Month, Date if (dateOne > dateTwo) { alert("Date One is greater than Date Two."); }else { alert("Date Two is greater than Date One."); } } CompareDate(); </script> Output ·
🌐
DEV Community
dev.to › onlinemsr › javascript-compare-dates-from-chaos-to-clarity-1g23
JavaScript Compare Dates: From Chaos to Clarity - DEV Community
May 6, 2024 - Do you want to learn how to compare dates in JavaScript? It’s easy and fun with the JavaScript Date object and the getTime() method. These are the best methods for working with dates and times in JavaScript. Let me show you why and how to use them. The Date object is a special kind of object that stores a date and time.
🌐
Zipy
zipy.ai › blog › how-to-compare-two-dates-with-javascript
how to compare two dates with javascript
April 12, 2024 - The simplest way to compare two dates in JavaScript is to use the greater than (>) and less than (<) operators.
🌐
Sentry
sentry.io › sentry answers › javascript › how to compare two dates with javascript
How to compare two dates with JavaScript | Sentry
When comparing Date objects, it’s best to first convert the Date to a timestamp number or a date string to avoid any issues with equality operators. If the Date constructor is called without the new operator, it ignores all arguments and returns ...
Find elsewhere
🌐
CodyHouse
codyhouse.co › blog › post › how-to-compare-dates-in-javascript
How to compare dates in JavaScript | CodyHouse
This is not an issue with the Date object itself, but rather a characteristic of how objects operate in JavaScript. If you need to use an equality operator to compare dates, then you can use the getTime() method of the Date object.
🌐
Stack Abuse
stackabuse.com › compare-two-dates-in-javascript
Compare Two Dates in JavaScript
February 24, 2023 - You're effectively comparing two integer counters: function dateCompare(d1, d2){ const date1 = new Date(d1); const date2 = new Date(d2); if(date1 > date2){ console.log(`${d1} is greater than ${d2}`) } else if(date1 < date2){ console.log(`${d2} is greater than ${d1}`) } else{ console.log(`Both dates are equal`) } } dateCompare("6/11/2020", "7/8/2019") dateCompare("01/01/2021", "01/01/2021") This results in: 6/11/2020 is greater than 7/8/2019 Both dates are equal ·
🌐
30 Seconds of Code
30secondsofcode.org › home › javascript › date › date comparison
How do I compare two dates in JavaScript? - 30 seconds of code
January 6, 2024 - This means that you can use the ... new Date('2020-10-21')); // true · Similarly, you can use the greater than operator (>) to check if a date comes after another date....
🌐
Javatpoint
javatpoint.com › javascript-compare-dates
JS Compare dates
January 23, 2020 - JavaScript Compare dates with javascript tutorial, introduction, javascript oops, application of javascript, loop, variable, data types, operators, javascript if, objects, map, typedarray etc.
🌐
UpStack
upstackhq.com › upstack blog › software development
How To Compare DateTime In JavaScript | Upstack
January var firstDate = new Date(2019, 3, 15); //Year, Month, Date var secondDate = new Date(2020, 3, 15); //Year, Month, Date if (firstDate > secondDate) { alert("First Date is greater than Second Date."); } else { alert("Second Date is greater than the First Date."); } } CompareDate(); Changing Date Format · We can use JavaScript code to change or format.
🌐
GeeksforGeeks
geeksforgeeks.org › compare-two-dates-using-javascript
JS Date Comparison – How to Compare Dates in JavaScript? | GeeksforGeeks
December 4, 2024 - JavaScript provides built-in methods for date comparisons, libraries like Moment.js and date-fns offer additional functionality and convenience.
🌐
Programmingwithswift
programmingwithswift.com › how-to-compare-dates-with-typescript
How to compare dates with TypeScript or JavaScript
September 11, 2021 - We can use the greater than check to see if one date is greater than another. This works too, as would be expected if the greater than check works. We have now seen that if we use toISOString() we can compare dates easily, no matter if it is ...
🌐
freeCodeCamp
freecodecamp.org › news › compare-two-dates-in-javascript
How to Compare Two Dates in JavaScript – Techniques, Methods, and Best Practices
February 12, 2024 - The code output for the third comparison shows that firstDate is later than or equal to the secondDate. The fifth comparison shows that firstDate is not equal to the secondDate. And the last comparison displayed that firstDate is not equal to the secondDate. It's important to note that comparison operators in JavaScript are based on the Coordinated Universal Time (UTC). If you want to compare dates based on their actual date and time values (including year, month, day, hours, minutes, seconds, and milliseconds), you may need to extract these components and compare them individually.
🌐
sebhastian
sebhastian.com › javascript-compare-dates
Date comparison in JavaScript with code examples | sebhastian
December 12, 2022 - Finally, the comparison operators greater than (>) and less than (<) are used to compare the two dates. The if and else-if conditions are set to print a message to the console indicating which date is earlier or later.
🌐
Codedamn
codedamn.com › news › javascript
JavaScript Date Comparison Guide with Examples
June 3, 2023 - The most straightforward approach to compare dates is by using direct comparison operators such as ==, !=, <, >, <=, and >=. However, it is essential to note that when comparing Date objects directly, JavaScript compares their references, not their values.
🌐
Reddit
reddit.com › r/learnjavascript › comparing two dates in javascript is easier than you may think.
r/learnjavascript on Reddit: Comparing two dates in JavaScript is easier than you may think.
May 11, 2022 - But +date is a number for sure, as long as you've ever seen coercion ... I learned something new today. Thank you! ... Doesn’t seem to make any difference. ... How about using == instead of ===? I think this way JS will compare the values returned by date1.valueOf() and date2.valueOf().
🌐
Udemy
blog.udemy.com › home › javascript date comparison: quick and simple
JavaScript Date Comparison: Quick and Simple - Udemy Blog
December 4, 2019 - It just uses an “if” conditional statement, along with the basic comparison operators (+, >, and <): “If rightnow is greater than backthen”… · In practice, of course, you’d probably be comparing dates that weren’t hard-coded, ...