Compare two Date strings using JavaScript

avatar

Borislav Hadzhiev

Mon Jan 24 20223 min read

banner

Photo by Ian Dooley

Compare two Date strings using JavaScript #

To compare two date strings:

  1. Pass the strings to the Date() constructor to create 2 Date objects.
  2. Compare the output from calling the getTime() method on the dates.
index.js
const dateStr1 = '2022-04-24'; const dateStr2 = '2022-09-21'; const date1 = new Date(dateStr1); const date2 = new Date(dateStr2); if (date1.getTime() === date2.getTime()) { console.log('dates are the same'); } else { // ✅ This runs 👇️ (dates are NOT the same) console.log('dates are not the same'); } if (date1.getTime() > date2.getTime()) { console.log('date1 comes after date2'); } else if (date1.getTime() < date2.getTime()) { // ✅ This runs 👇️ (date2 comes after) console.log('date2 comes after date1'); } else { console.log('dates are the same'); }

We passed the date strings to the Date() constructor to create 2 Date objects.

If passing the date string to the Date() constructor does not return a valid date, then you have to format your date strings differently, e.g. yyyy-mm-dd (more on that below).

The getTime method returns a timestamp of the elapsed milliseconds between the 1st of January 1970 00:00:00 and the given date.

A greater number means that more time has passed since the Unix Epoch, therefore the date is greater.

We compared 2 dates - the 24th of April, 2022 and the 21st of September 2022.

In our first first if statement, we check if the date strings point to the same year, month and day of the month, which is not the case, so our else block runs.

In the second example, the else if block runs because the second date comes after the first.

It should be noted that you don't have to explicitly call the getTime() method when comparing the dates.

index.js
const dateStr1 = '2022-04-24'; const dateStr2 = '2022-09-21'; const date1 = new Date(dateStr1); const date2 = new Date(dateStr2); if (date1 > date2) { console.log('date1 comes after date2'); } else if (date1 < date2) { // ✅ This runs 👇️ (date2 comes after) console.log('date2 comes after date1'); } else { console.log('dates are the same'); }
Each date stores a timestamp under the hood, so the default behavior is to compare the timestamps of the dates even if you don't explicitly call the getTime() method on each date.

Which approach you pick is a matter of personal preference.

If you have difficulties creating a valid Date object from your date strings, you can pass 2 types of parameters to the Date() constructor:

  1. a valid ISO 8601 string, formatted as YYYY-MM-DDTHH:mm:ss.sssZ, or just YYYY-MM-DD, if you only have a date without time.
  2. multiple, comma-separated parameters that represent the year, month (0 = January to 11 = December), day of the month, hours, minutes and seconds.

Here is an example that splits a string and passes the parameters to the Date() constructor to create a Date object.

index.js
// 👇️ Formatted as MM/DD/YYYY const str = '07/21/2022'; const [month, day, year] = str.split('/'); // 👇️ Create valid Date object const date = new Date(+year, month - 1, +day); console.log(date); // 👉️ Thu Jul 21 2022

The date string is formatted as mm/dd/yyyy, but the approach applies to any other format.

We split the string on each forward slash to get an array of substrings.

index.js
const str = '07/21/2022'; // 👇️ ['07', '21', '2022'] console.log(str.split('/'))

We used array destructuring to assign the month, day and year values to variables and passed them to the Date() constructor.

Once the Date object is created, all you have to do to compare the dates is compare the output from their getTime methods.

index.js
const str = '07/21/2022'; // 👇️ ['07', '21', '2022'] console.log(str.split('/')); const [month, day, year] = str.split('/'); // 👇️ Create valid Date object const date = new Date(+year, month - 1, +day); console.log(date); // 👉️ Thu Jul 21 2022 const now = new Date(); if (date.getTime() > now.getTime()) { console.log('date is in the future'); }

Notice that we subtracted 1 from the month when passing it to the Date() constructor.

This is because, the Date constructor expects a zero-based value, where January = 0, February = 1, March = 2, etc.
Use the search field on my Home Page to filter through my more than 1,000 articles.