How to Compare two (2) Dates in JavaScript?

Discover the various methods of how to compare two dates in JavaScript.

This article provides a comprehensive guide working with dates and times in JavaScript, including examples and solutions for comparing two dates.

So keep reading to learn new insights on comparing dates in JavaScript that will enhance your skills!

What is Date object in JavaScript?

The Date object is a built-in object in JavaScript that represents a single moment in time.

It is based on a time value that is the number of milliseconds since midnight of January 1, 1970, UTC (Coordinated Universal Time).

The Date object provides methods for working with dates and times, such as getting the current date and time, setting the date and time, and comparing dates.

Here’s an example of how to create a new Date object representing the current date and time:

var currentDate = new Date();
console.log(currentDate);

This code creates a new Date object using the new Date() constructor. The currentDate variable now holds a reference to this Date object.

We can then use the console.log() method to print the current date and time to the console.

Output:

2023-08-02T07:21:41.899Z

Solution on how to Compare two (2) Dates in JavaScript?

The following are a few ways to compare two dates in JavaScript:

Solution 1: Use comparison operators

You can use the comparison operators <, >, <=, and >= to compare two Date objects.

Here’s an example of how to compare two dates in JavaScript using the Date object:

var date1 = new Date("August 02, 2022");
var date2 = new Date("August 02, 2023");

if (date1 < date2) { console.log("date1 is earlier than date2"); } else if (date1 > date2) {
console.log("date1 is later than date2");
} 

In this example, we create two Date objects representing two different dates. We can then use comparison operators such as <, > to compare the two dates.

Output:

date1 is earlier than date2

Here’s another example:

var date1 = new Date("2022-08-02");
var date2 = new Date("2023-08-02");

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");
}

Here, we create two Date objects representing two different dates and we use comparison operators such as <, >, and == to compare the two dates.

Output:

date1 is earlier than date2

Take note, using equality operators (==, !=, ===, or !==) does not work when you are comparing two Date objects.

This is because the equality operators in JavaScript do not function as expected when comparing two Date objects due to the comparison being based on reference rather than value.

Therefore, two Date variables are not considered equal unless they refer to the same Date object stored in memory.

To accurately compare two Date objects, you can convert them to a common format such as a timestamp or a date string. To avoid any problems with equality operators when comparing Date objects.

For example, you can use the getTime() method to get the time value of a Date object, then compare these time values using comparison operators.

Here’s an example:

var date1 = new Date("2023-08-02");
var date2 = new Date("2023-08-02");

if (date1.getTime() === date2.getTime()) {
console.log("date1 and date2 are equal");
} else {
console.log("date1 and date2 are not equal");
}

As you can see, we create two Date objects representing the same date.

Then, we use the getTime() method to get their time values in milliseconds and compare these values using the === operator.

Output:

date1 and date2 are equal

Solution 2: Use the getTime() method

You can also use the getTime() method to compare time values using comparison operators.

Here’s an example:

var date1 = new Date("2024-08-02");
var date2 = new Date("2023-08-02");

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");
}

Output:

date1 is later than date2

Solution 3: Use the valueOf() method:

The valueOf() method returns the primitive value of a Date object. You can use this method to compare two dates by comparing their primitive values.

Here’s an example:

var date1 = new Date("2023-08-02");
var date2 = new Date("2023-08-03");

if (date1.valueOf() < date2.valueOf()) {
    console.log("date1 is earlier than date2");
} else if (date1.valueOf() > date2.valueOf()) {
    console.log("date1 is later than date2");
} else {
    console.log("date1 and date2 are the same");
}

Solution 4: Use the toISOString() method

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ.

You can use this method to compare two dates by comparing their ISO strings.

Here’s an example:

var date1 = new Date("2023-08-04");
var date2 = new Date("2023-08-03");

if (date1.toISOString() < date2.toISOString()) {
    console.log("date1 is earlier than date2");
} else if (date1.toISOString() > date2.toISOString()) {
    console.log("date1 is later than date2");
} else {
    console.log("date1 and date2 are the same");
}

Output:

date1 is later than date2

How to compare a date with the current date in JavaScript

You can compare a date with the current date in JavaScript by creating a new Date object representing the current date and time.

And then comparing it with another Date object using comparison operators or methods such as getTime() or valueOf().

Here’s an example:

var currentDate = new Date();
var someDate = new Date("2023-08-01");

if (someDate < currentDate) { console.log("someDate is earlier than the current date"); } else if (someDate > currentDate) {
console.log("someDate is later than the current date");
} else {
console.log("someDate and the current date are the same");
}

In this example, we create a new Date object representing the current date and time using the new Date() constructor. We also create another Date object representing a specific date.

Then use comparison operators such as <, >, and == to compare the two dates.

Output:

someDate is earlier than the current date

Conclusion

In conclusion, this article provides a comprehensive guide on how to compare two dates in JavaScript.

Along with various methods, including using comparison operators, getTime() method, valueOf() method, and toISOString() method.

It emphasizes the importance of converting Date objects into a common format, such as timestamps or date strings, to ensure accurate and reliable comparisons.

We are hoping that this article provides you with enough information that help you understand on how to compare dates in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.

Caren Bautista


Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel
 · View all posts by Caren Bautista →

Leave a Comment