How to compare Dates in TypeScript

avatar

Borislav Hadzhiev

Last updated: Apr 16, 2022

banner

Check out my new book

Compare Dates in TypeScript #

To compare dates in TypeScript:

  1. Call the getTime() method on each date to get a timestamp.
  2. Compare the timestamp of the dates.
  3. If a date's timestamp is greater than another's, then that date comes after.
index.ts
const date1 = new Date('2022-04-16T06:55:31.820Z'); const date2 = new Date('2022-04-26T09:30:24.820Z'); // ✅ compare dates in TypeScript ✅ if (date1.getTime() === date2.getTime()) { console.log('dates are the same'); } else { console.log('dates are NOT the same'); } if (date1.getTime() > date2.getTime()) { console.log('date1 comes after date2'); } else if (date1.getTime() < date2.getTime()) { console.log('date2 comes after date1'); } else { console.log('dates are the same'); } // --------------------------------------------------------- // ✅ compare dates WITHOUT time in TypeScript ✅ const date1WithoutTime = new Date(date1.getTime()); const date2WithoutTime = new Date(date2.getTime()); date1WithoutTime.setUTCHours(0, 0, 0, 0); date2WithoutTime.setUTCHours(0, 0, 0, 0); if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { console.log('dates are the same'); } else { console.log('dates are not the same'); } if (date1WithoutTime.getTime() > date2WithoutTime.getTime()) { console.log('date1 comes after date2'); } else if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { console.log('date1 and date2 are the same'); } else { console.log('date2 comes after date1'); }

The code snippet shows 2 examples:

  1. How to compare dates in TypeScript including the time.
  2. How to compare dates in TypeScript without the time.

We used the Date() constructor to create 2 date objects. The first date is the 16th of April 2022, and the second - the 26th of April 2022.

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

If the timestamp of one date is greater than the timestamp of another, then the first date comes after the second.

On the other hand, if the timestamps of the two dates are equal, then the two dates represent the exact same moment in time (including hours, minutes, seconds and milliseconds).

You can compare the output from getTime() just like you would compare numbers in TypeScript.

Sometimes you need to compare two dates while ignoring the time components.

To compare two dates without the time in TypeScript:

  1. Create a copy of each date.
  2. Use the setUTCHours() method to set the time on the copied dates to midnight.
  3. Compare the output from calling the getTime() method on the dates.
index.ts
const date1 = new Date('2022-04-16T06:55:31.820Z'); const date2 = new Date('2022-04-16T09:30:24.820Z'); // ✅ compare dates WITHOUT time in TypeScript const date1WithoutTime = new Date(date1.getTime()); const date2WithoutTime = new Date(date2.getTime()); date1WithoutTime.setUTCHours(0, 0, 0, 0); date2WithoutTime.setUTCHours(0, 0, 0, 0); if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { console.log('dates are the same'); } else { console.log('dates are not the same'); } if (date1WithoutTime.getTime() > date2WithoutTime.getTime()) { console.log('date1 comes after date2'); } else if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) { console.log('date1 and date2 are the same'); } else { console.log('date2 comes after date1'); }

The two date objects in the example represent the 16th of April 2022.

To be able to compare the dates and ignore the time, we have to reset the time of the dates to midnight (or simply the same hour, minutes, seconds and milliseconds).

The setUTCHours method takes the hours, minutes, seconds and milliseconds as parameters and sets the values on the given date according to universal time.

However, the setUTCHours method mutates the Date object in place, which might not be what you want.

This is why we used the getTime method to get a timestamp of the elapsed milliseconds between the 1st of January 1970 00:00:00 and the given date.

We can pass the timestamp to the Date() constructor to create a copy of the Date object.

We used the setUTCHours method to set the time for the dates to midnight, so we can compare the dates ignoring the time.

The getTime method returns a number that represents the milliseconds elapsed between the Unix Epoch and the given date.

index.js
// 👇️ 1650094409568 console.log(new Date().getTime());

If we have two dates that store the same year, month and day of the month, then the timestamps are going to be equal because we set the time on both dates to midnight.

If one date is greater than another, then its timestamp is going to store a larger number because more time has elapsed between the Unix Epoch and the specific date.

Use the search field on my Home Page to filter through my more than 3,000 articles.