How to compare two dates in JavaScript

This post is really one of my favourite posts because in this post I am going to show you how to compare two dates in JavaScript. I have found a lot of solutions to do it. But as this is CodeSpeedy and I always give you the easiest and best way to solve any problem, thus I need to think for a while which will be the best for you.

And I came to know that there are mainly three ways to compare dates in JavaScript.

Calculate the difference between two dates in PHP

Compare two dates in JavaScript with various methods

At first, we gonna start with the easiest method.

Comparing two dates in JavaScript can be said like this:

Compare two dates in terms of which is greater and which is smaller.

In order to do that we can use these below relational operators:

<
>
<=
>=

But unfortunately, we can not use these below operators:

==
!=
!==
===

You can’t use these to compare the value of the date.

To make you understand these I am gonna give you some example,

Example of comparing two dates in JavaScript

Example No 1:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <script type="text/javascript">
    var date1 = new Date('2018-05-26');
    var date2 = new Date('2018-05-23');
    if(date1>date2){
      document.write('true');
    }
    else
      document.write('false');
  </script>
</body>
</html>

Output:

true

Example no 2:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <script type="text/javascript">
    var date1 = new Date('2018-05-26');
    var date2 = new Date('2018-05-23');
    if(date1<date2){
      document.write('true');
    }
    else
      document.write('false');
  </script>
</body>
</html>

Output:

false

How to get last modification time of a file in PHP

Example No 3:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <script type="text/javascript">
    var date1 = new Date('2018-05-26');
    var date2 = new Date('2018-05-23');
    if(date1<=date2){
      document.write('true');
    }
    else
      document.write('false');
  </script>
</body>
</html>

Output:

false

Example No 4:

<!DOCTYPE html>
<html>
<head>
  <title></title>
</head>
<body>
  <script type="text/javascript">
    var date1 = new Date('2018-05-26');
    var date2 = new Date('2018-05-23');
    if(date1>=date2){
      document.write('true');
    }
    else
      document.write('false');
  </script>
</body>
</html>

Output:

true

Get user’s Latitude and Longitude in JavaScript &#8211; HTML5 Geolocation

Till now this is fine. But as I said before, you can’t use some operators to compare the value of a date.

Here is some example of why you can not use those operators to compare dates in JavaScript

If you take two same dates and compare those using == operator you will get false as output. Let’s see this

var date1 = new Date('2018-05-23');
var date2 = new Date('2018-05-23');
	if(date1==date2){
		document.write('true');
	}
	else
		document.write('false');

Output:

false

You can check other operators I have mentioned above.

How to use == , === , != and !== to compare dates in JavaScript

 

Still, if you want to compare those operators which I told you can not use. Just follow this

Use getTime()

var date1 = new Date('2018-05-23');
var date2 = new Date('2018-05-23');
if(date1.getTime()==date2.getTime()){
document.write('true');
}
else
document.write('false');

Output:

true

using getTime() you can compare dates with those operators.

3D Photo/Image Gallery (on space) Using HTML5 CSS JS

2 responses to “How to compare two dates in JavaScript”

  1. Serena says:

    Why is this the #5 search result?!!! ARRGH! I’ve been wondering why == didn’t work for me ?
    Seriously, this is a great post. Thank you for taking the time to explain this.

    • Saruque Ahamed Mollick says:

      Thank you very much that it helped you out. Actually this is a new site still. With your love, we can provide more useful tutorials.

Leave a Reply

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