3 Ways To Compare Dates In Javascript – Simple Examples

Welcome to a tutorial on how to compare dates in Javascript. Have a couple of dates in Javascript that you need to compare?

There are a few common ways to compare dates in Javascript:

  1. Parse the date strings into date objects and compare them directly using operators.
    • var dateA = new Date("1 Feb 2003");
    • var dateB = new Date("2 Mar 2004");
    • if (dateA > dateB) { A IS LATER } else { B IS LATER OR EQUAL }
  2. Parse the date strings into Unix Timestamps and compare them.
    • var dateA = new Date("3 Apr 2005").getTime();
    • var dateB = new Date("4 May 2006").getTime();
    • if (dateA > dateB) { A IS LATER } else { B IS LATER OR EQUAL }
  3. Lastly, parse the dates into integers if they are in the format of YYYY-MM-DD:
    • var dateA = "5 Jun 2007";
    • var dateB = "6 Jul 2008";
    • dateA = parseInt(dateA.replaceAll("-", ""));
    • dateB = parseInt(dateB.replaceAll("-", ""));
    • if (dateA > dateB) { A IS LATER } else { B IS LATER OR EQUAL }

That should cover the basics, but read on for more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Compare Dates Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

COMPARE DATES

All right, let us now get into the examples of comparing dates in Javascript.

 

1) COMPARE DATE OBJECTS

1-date-object.html
// (A) PARSE DATES INTO OBJECTS
var dateA = new Date("18 Aug 2020");
var dateB = new Date("14 Aug 2020");

// (B) DIRECTLY COMPARE USING OPERATORS
if (dateA > dateB) { console.log("A is later"); }
else if (dateB > dateA) { console.log("B is later"); }
else { console.log("AB are equal"); }

This is as straightforward as it gets – Parse the date strings into objects, then directly compare them using the comparison operators.

 

2) COMPARE UNIX TIMESTAMPS

2-unix.html
// (A) PARSE DATES INTO OBJECTS
var dateA = new Date("14 Aug 2020");
var dateB = new Date("18 Aug 2020");

// (B) TO UNIX TIMESTAMPS
var unixA = dateA.getTime();
var unixB = dateB.getTime();

// (C) COMPARE USING OPERATORS
if (unixA > unixB) { console.log("A is later"); }
else if (unixB > unixA) { console.log("B is later"); }
else { console.log("AB are equal"); }

This is probably the most common example that is floating all around the Internet. For you guys who have not heard, the Unix Timestamp is simply the number of seconds that have elapsed since 1 Jan 1970. So in this one, we are just parsing the date strings into Unix Timestamps and comparing them.

You sharp ones should have also noticed. We are actually parsing the date strings into objects first, then into Unix Timestamps – new Date("DATE").getTime(). This is a “useless extra step” in modern Javascript since we can now directly compare the date objects.

 

 

3) COMPARE DATE INTEGERS

3-parse-int.html
// (A) DATE STRINGS TO INTEGER
// NOTE: MUST BE IN FORMAT OF YYYY-MM-DD
var dateA = "2019-08-07";
var dateB = "2018-07-06";
dateA = parseInt(dateA.replaceAll("-", ""));
dateB = parseInt(dateB.replaceAll("-", ""));
 
// (B) COMPARE USING OPERATORS
if (dateA > dateB) { console.log("A is later"); }
else if (dateB > dateA) { console.log("B is later"); }
else { console.log("AB are equal"); }

Here is an alternative for you guys who are working with YYYY-MM-DD date format – Simply parse the string into an integer and compare them.

 

4) COMPARE MULTIPLE DATES WITH ARRAY SORT

4-array.html
// (A) ARRAY OF DATES
var dates = ["8 Sep 2010", "2012-11-10", "2020-07-06", "5 Jun 2017"];

// (B) PARSE INTO OBJECTS
for (let i in dates) {
  dates[i] = new Date(dates[i]);
}

// (C) ARRAY SORT
dates.sort();

// (D) LATEST TO EARLIEST
console.log("LATEST: " + dates[0]);
console.log("EARLIEST: " + dates[dates.length-1]);

What if we have multiple date strings to compare? This last example is a simple trick that I always use – Parse all of them into an array and simply use sort().

 

 

USEFUL BITS & LINKS

That’s all for the main tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Compare Dates In Javascript (click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

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