Try using DateJS, an open-source JavaScript Date Library that can handle pretty much everything! The following example is working:

<script type="text/javascript" src="date.js"></script>
<script>

    startdate = "2009-11-01";
    enddate  = "2009-11-04"; 

    var d1 = Date.parse(startdate);
    var d2 = Date.parse(enddate) ;

    if (d1 < d2) {
        alert ("Error!");
    }

</script>
Answer from Filipe Miguel Fonseca on Stack Overflow
🌐
W3Resource
w3resource.com › javascript-exercises › javascript-date-exercise-5.php
JavaScript: Comparison between two dates - w3resource
July 17, 2025 - const is_after_date = (date1, date2) => date1 > date2; console.log("Is 2018/8/15 after 2018/8/21?") console.log(is_after_date(new Date(2018, 8, 15), new Date(2018, 8, 21))); console.log("Is 2018/8/21 after 2018/8/15?") console.log(is_after_date(new Date(2018, 8, 21), new Date(2018, 8, 15))); ... See the Pen JavaScript - Comparison between two dates-date-ex- 5 by w3resource (@w3resource) on CodePen.
Top answer
1 of 7
11

Try using DateJS, an open-source JavaScript Date Library that can handle pretty much everything! The following example is working:

<script type="text/javascript" src="date.js"></script>
<script>

    startdate = "2009-11-01";
    enddate  = "2009-11-04"; 

    var d1 = Date.parse(startdate);
    var d2 = Date.parse(enddate) ;

    if (d1 < d2) {
        alert ("Error!");
    }

</script>
2 of 7
3

Someone finally uses ISO 8601 standard dates but then ...

You are using a nice international standard that JavaScript arguably should understand. But it doesn't.

The problem is that your dates are in ISO 8601 standard format which the built-in Date.parse() can't read.

JavaScript implements the older IETF dates from RFC 822/1123. One solution is to tweak them into the RFC-style, which you can see in RFC1123, and which look like dd month yyyy.

There is coding floating about that can scan the ISO format comprehensively, and now that you know to google for "iso standard date" you can get it. Over here I found this:

Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    this.setTime(Number(time));
}

js> t = new Date()
Sun Nov 01 2009 09:48:41 GMT-0800 (PST)
js> t.setISO8601("2009-11-01")
js> t

Sat Oct 31 2009 17:00:00 GMT-0700 (PDT)

The 11-01 is reinterpreted in my timezone, as long as all your dates get the same conversion then they should compare reasonably, otherwise you can add TZ info to your string or to the Date object.

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 › compare-two-dates-in-javascript
How to Compare Two Dates in JavaScript – Techniques, Methods, and Best Practices
February 12, 2024 - The standard comparison operators (<, >, ===) were used to determine their relationship. The output of the above code was firstDate is later than secondDate, because the secondDate comes before the firstDate.
🌐
GeeksforGeeks
geeksforgeeks.org › compare-two-dates-using-javascript
JS Date Comparison – How to Compare Dates in JavaScript? | GeeksforGeeks
December 4, 2024 - When comparing dates in JavaScript, it is important to understand that the Date objects represent points in time.
🌐
InfluxData
influxdata.com › home › comparing two dates in javascript: a guide
Comparing Two Dates in JavaScript: A Guide | InfluxData
June 26, 2024 - ... Remember that months are zero-based when passed as parameter values. Don’t use the equality operator to compare two Date objects; instead, convert them with getCurrentTime() and then make the comparison.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › compare-two-dates-using-javascript
JS Date Comparison - How to Compare Dates in JavaScript? - GeeksforGeeks
July 11, 2025 - When comparing dates in JavaScript, it is important to understand that the Date objects represent points in time.
🌐
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, you have learned how to do date comparisons in JavaScript using the date Object without having to install any library.
🌐
Blogger
gregoryboxij.blogspot.com › home › compare two dates in javascript dd/mm/yyyy › compare two dates in javascript w3schools › compare two dates using javascript
40 Compare Two Dates Using Javascript - Modern Javascript Blog
November 21, 2021 - First, we can convert the Date into a numeric value by using getTime () function. By converting the given dates into numeric value we can directly compare them. Compare two dates using javascript.
Find elsewhere
🌐
CodyHouse
codyhouse.co › blog › post › how-to-compare-dates-in-javascript
How to compare dates in JavaScript | CodyHouse
A tutorial on using the Date() object and its methods to perform date comparison in JavaScript.
🌐
Scaler
scaler.com › home › topics › how to compare two dates in javascript?
How to Compare Two Dates With JavaScript? - Scaler Topics
February 27, 2024 - Comparison operators like > or < can be used to compare dates in JavaScript, while the getTime() method directly compares two dates.
🌐
Stack Abuse
stackabuse.com › compare-two-dates-in-javascript
Compare Two Dates in JavaScript
February 24, 2023 - In this tutorial, we'll go over how to compare dates in vanilla JavaScript, using the built-in methods and operators, with examples.
🌐
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 - Combining the previous two snippets, you can check if a date is between two other dates. const isBetweenDates = (dateStart, dateEnd, date) => date > dateStart && date < dateEnd; isBetweenDates( new Date('2020-10-20'), new Date('2020-10-30'), new Date('2020-10-19') ); // false isBetweenDates( new Date('2020-10-20'), new Date('2020-10-30'), new Date('2020-10-25') ); // true ... Comparing values in JavaScript is one of the most common tasks, yet it has a lot of things you should bear in mind.
🌐
Geshan
geshan.com.np › blog › 2024 › 11 › javascript-compare-dates
A Beginner's Guide to Comparing Dates in JavaScript
November 1, 2024 - If your project is an npm project ... from date-fns to compare dates. The compareAsc function in date-fns compares two dates and returns a number that indicates their relative order....
🌐
Sentry
sentry.io › sentry answers › javascript › how to compare two dates with javascript
How to compare two dates with JavaScript | Sentry
Equality operators don’t work when comparing two Date objects, because JavaScript objects are compared by reference, not value. Two Date variables are not considered to be the same unless they point to the same Date object in memory.
🌐
WsCube Tech
wscubetech.com › resources › javascript › programs › compare-two-dates
How to Compare Two Dates in JavaScript? (7 Programs)
October 31, 2025 - Explore 7 simple JavaScript programs to compare two dates. Learn how to use the getTime() method, valueOf(), relational operators, and more.
🌐
Zipy
zipy.ai › blog › how-to-compare-two-dates-with-javascript
how to compare two dates with javascript
April 12, 2024 - In this example, we calculate the difference between date1 and date2 in milliseconds, then convert it to days by dividing by the number of milliseconds in a day. The result shows the number of days between the two dates. When comparing dates, it's essential to consider whether you need to include or exclude the time components (hours, minutes, seconds, and milliseconds).
🌐
Bacancy Technology
bacancytechnology.com › qanda › javascript › compare-two-dates-with-javascript
How to Compare Two Dates With JavaScript: A Complete Guide
// Create two date objects const ... dates if (date1 > date2) { console.log('date1 is later than date2'); } else if (date1 < date2) { console.log('date1 is earlier than date2'); } else { console.log('date1 is equal to date2'); }...
🌐
Educative
educative.io › answers › how-to-compare-dates-using-javascript
How to compare dates using JavaScript
To perform equality comparisons in JavaScript, utilize the Date object along with the getTime() method, which returns the number of milliseconds. For more specific details such as the day, month, and more, employ additional date methods like ...
🌐
Medium
medium.com › @ryan_forrester_ › comparing-dates-in-javascript-how-to-guide-66e3f1be33d8
Comparing Dates in JavaScript (How to Guide) | by ryan | Medium
September 13, 2024 - You can use comparison operators (<, <=, >, >=, ==, ===, !=, !==) to compare date objects directly.