How To Compare Two Date Strings In JavaScript

How To Compare two Date strings in JavaScript

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:

Leave a Reply

Your email address will not be published. Required fields are marked *