To compare two date strings in JavaScript, we have three methods: Using the Date.parse()
method, creating your own function to compare, and using the getTime()
method. Check out the details below to get more information.
Compare two date strings in JavaScript
Using the Date.parse() method
Comparing two date strings in JavaScript is standard programming that programmers usually do. To compare two date strings in Javascript, we can easily use the Date.parse()
method to do it. This function is already built-in in Javascript.
Syntax:
Date.parse(datestring)
Parameter:
- datestring: A string representing a date.
Return value: A number.
Example:
In this example, we will use the Date.parse()
method to parse a date string and return the time.
// Use the Date.parse() method to convert date string to a number and compare two numbers var dateOct = Date.parse("2022-10-01"); var dateNov = Date.parse("2022-11-01"); console.log(dateOct); if (dateNov > dateOct) { console.log("dateOct is greater than dateNov"); }
Output
1664582400000
dateOct is greater than dateNov
Creating the function to compare
The other simple way is creating your own function to compare two date strings. Let’s see the example below for more details.
Example
// Create a function named 'compareTwoDates' with two inputs: 'dateString1' and 'dateString2' function compareTwoDates(dateString1, dateString2) { if (dateString1 > dateString2) { console.log(dateString1, "is greater than", dateString2); } else if (dateString1 === dateString2){ console.log(dateString1, "equals to", dateString2); } else { console.log(dateString1, "is less than", dateString2); } } compareTwoDates("2022-12-01", "2022-11-01"); compareTwoDates("2022-11-01", "2022-11-01"); compareTwoDates("2022-10-01", "2022-11-01");
Output
2022-12-01 is greater than 2022-11-01
2022-11-01 equals to 2022-11-01
2022-10-01 is less than 2022-11-01
Using the getTime() method
There is another method is to use the getTime()
function. The getTime()
method will return the number of milliseconds since January 1, 1970.
Syntax:
getTime()
Parameter: None.
Return value: The number of milliseconds since January 1, 1970.
Example
var dateString1 = new Date('January 01, 2022 00:00:00'); var dateString2 = new Date('July 20, 2016 00:00:00'); // We compare two date strings use getTime() method if (dateString1.getTime() === dateString2.getTime()) { console.log("dateString1 and dateString2 is the same date"); } if (dateString1.getTime() > dateString2.getTime()) { console.log("dateString1 is newer than dateString2"); }
Output
dateString1 is newer than dateString2
Summary
In this tutorial, we have explained how to compare two date strings in JavaScript. We always hope this tutorial is helpful to you. Leave your comment here if you have any questions or comments to improve the article. Thanks for reading!
Maybe you are interested:
- How To Convert Milliseconds To A Date Using JavaScript
- How To Get the Number of Days in a Month in JavaScript
- Get Date and Time in User’s locale format in JavaScript

My name is Fred Hall. My hobby is studying programming languages, which I would like to share with you. Please do not hesitate to contact me if you are having problems learning the computer languages Java, JavaScript, C, C#, Perl, or Python. I will respond to all of your inquiries.
Name of the university: HUSC
Major: IT
Programming Languages: Java, JavaScript, C , C#, Perl, Python