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

🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Date
Date - JavaScript | MDN
Note: With the introduction of the Temporal API, the Date object is considered a legacy feature. Consider using Temporal for new code and migrate existing code over to it if possible (check the browser compatibility. We will be writing a usage guide soon! A JavaScript date is fundamentally specified as the time in milliseconds that has elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC (equivalent to the UNIX epoch).
🌐
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.
🌐
InfluxData
influxdata.com › home › comparing two dates in javascript: a guide
Comparing Two Dates in JavaScript: A Guide | InfluxData
June 26, 2024 - You can then use this number to perform the comparison: ... Number is a primitive type, so the equality operator behaves as you would expect, and the code displays Equal. You’ve just seen that using the equality operator to compare dates in JavaScript doesn’t work—unless you’re trying to compare their references.
🌐
Geshan
geshan.com.np › blog › 2024 › 11 › javascript-compare-dates
A Beginner's Guide to Comparing Dates in JavaScript
November 1, 2024 - For a reliable date comparison in JavaScript, it is recommended to use the getTime() method available on the Date object. This is because when you compare Date objects directly, they are compared based on references, not their actual value.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › compare-two-dates-using-javascript
JS Date Comparison - How to Compare Dates in JavaScript? - GeeksforGeeks
July 11, 2025 - JavaScript provides built-in methods for date comparisons, libraries like Moment.js and date-fns offer additional functionality and convenience.
Find elsewhere
🌐
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 - Because you are not comparing values but reference. date1 has a different location (reference) in the memory as date 2 even though their values are the same. In the second example that returns true, you are comparing actual values. Look up on youtube reference vs value in javascript, if you get a visual explanation it will be easier to understand.
🌐
freeCodeCamp
freecodecamp.org › news › compare-two-dates-in-javascript
How to Compare Two Dates in JavaScript – Techniques, Methods, and Best Practices
February 12, 2024 - JavaScript internally converts the dates (milliseconds since January 1, 1970) to their respective corresponding timestamps. The below code shows a date comparison using the the comparison operators:
🌐
Educative
educative.io › answers › how-to-compare-dates-using-javascript
How to compare dates using JavaScript
One approach to compare two dates in JavaScript involves converting them into numeric values that represent their respective time. Using the getTime() function, we can transform the Date object into a numeric value.
🌐
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 - You can also use plugins to add more features, like natural language comparisons, relative time, or custom formats. Using date-fns is easy. Just install it and import it into your project. Here’s how: // Install date-fns using npm npm install date-fns // Import date-fns in your JavaScript file const { compareAsc, compareDesc, format, formatDistance, formatRelative, isAfter, isBefore, isEqual, isSameDay, isSameMonth, isSameYear } = require("date-fns");
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Temporal › PlainDateTime › compare
Temporal.PlainDateTime.compare() - JavaScript | MDN
December 8, 2025 - The Temporal.PlainDateTime.compare() static method returns a number (-1, 0, or 1) indicating whether the first date-time comes before, is the same as, or comes after the second date-time. Equivalent to first comparing their dates, then comparing their times if the dates are the same.
🌐
Bitstack
blog.bitsrc.io › how-to-slash-through-node-js-date-comparison-pitfalls-43d7939b0d06
How to Slash Through Node.js Date Comparison Pitfalls | by Dureksha Wasala | Medium
November 12, 2024 - When it comes to handling date comparisons with Node.js, you might assume that going for an actual date is easier than this 😅. This post provides valuable tips and tricks for mastering date comparisons, enabling you to handle complex tasks such as calendar scheduling and filtering appointments with ease. Understanding how Javascript represents dates is the first step to using them wisely.
🌐
Sentry
sentry.io › sentry answers › javascript › how to compare two dates with javascript
How to compare two dates with JavaScript | Sentry
January 30, 2023 - The Date object is used to represent a single moment in time and has methods for formatting dates and doing time zone conversions. Next, you can compare the two Date objects using comparison operators (>, <, =, and >=):
🌐
UpStack
upstackhq.com › upstack blog › software development
How To Compare DateTime In JavaScript | Upstack
An excellent approach to compare dates is using the getTime() function. This function allows you to convert dates to numeric values for direct comparison.
🌐
W3Schools
w3schools.com › js › js_dates.asp
JavaScript Dates
February 4, 2026 - const d = new Date(2018, 11, 24, 10, 33, 30, 0); Try it Yourself » ... January = 0. December = 11. Specifying a month higher than 11, will not result in an error but add the overflow to the next year: ... Specifying a day higher than max, will not result in an error but add the overflow to the next month: ... You cannot omit month. If you supply only one parameter it will be treated as milliseconds. ... JavaScript stores dates as number of milliseconds since January 01, 1970.
🌐
CoreUI
coreui.io › blog › how-to-compare-dates-with-javascript
How to compare dates with JavaScript · CoreUI
January 23, 2024 - ... Note that JavaScript months are zero-indexed, meaning January is 0, February is 1, and so on up to December being 11. JavaScript’s comparison operators (>, <, ==, ===, !=, !==) can be used to compare two Date objects directly.
🌐
Zipy
zipy.ai › blog › how-to-compare-two-dates-with-javascript
how to compare two dates with javascript
April 12, 2024 - In this article, we'll explore various techniques for comparing dates in JavaScript, ranging from simple comparisons to more advanced scenarios.
🌐
CodyHouse
codyhouse.co › blog › post › how-to-compare-dates-in-javascript
How to compare dates in JavaScript | CodyHouse
July 17, 2023 - A tutorial on using the Date() object and its methods to perform date comparison in JavaScript.
🌐
Reddit
reddit.com › r/learnprogramming › compare dates in javascript
r/learnprogramming on Reddit: Compare Dates in Javascript
October 4, 2023 -

Good day all,

I'm a network security engineer by trade however have a small amount of programming knowledge so i'm attempting to write some Javascript that takes a ticket displayed in a ticket queue and based on its follow-up date set by the aligned engineer, appears green, yellow or red.

The issue I face, is formatting/normalising the dates so I can conduct a comparison. I need to decide on my approach here as I know it can be done in different ways with varying degrees of difficulty.

Ideally I would format today's date into '02 Oct, 2023 - 11:00' format, however I also need to separate the time so I can do time comparisons for tickets that have intraday deadlines. I know I can use the split() function to achieve this and save the value I want as a variable - but still insure on how to actually compare the dates themselves and which way round to normalise would be easiest.

As can be seen below, the follow-up date from the ticketing system is stored as a title element.

<div class="ellipsis" data-test-id="date-cell" title="02 Oct, 2023 11:00">

02 Oct, 2023 11:00

</div>

For example, lets say today's date is '04/10/2023 10:30' and I need to compare that to '02 Oct, 2023 11:00' - how would you go about doing this?

Thanks in advance for your support, no doubt you can help a poor old netsec engineer save some time.