How to compare two dates with or without time in Typescript?

Typescript Date Comparison: This tutorial explains how to compare dates in Angular and TypeScript.

When comparing dates to see if they are the same, logic for comparison must be used.

If two objects are equal, it means their values or property values are equal.

In the case of the Date object, it contains the date and time in milliseconds.

Dates in TypeScript are represented using the Date type.

Let’s create a Date object and print the date using console.log statement:

console.log(new Date()); //Mon Jun 28 2021 10:51:01 GMT+0530 (India Standard Time)

The Date object returns default values containing:

  • Week name
  • Date
  • Time
  • Time zone

Let’s create two dates and compare them using the equal comparison operator == or ===

let date1 = new Date("2021-02-21");
let date2 = new Date("2021-02-21");
console.log(date1 === date2); // false
console.log(date1 == date2); // false

Even though we created two dates, when we compared using equal operators, it returns false.

This post answers the following questions:

  • How to compare dates with time?
  • How to compare dates without time?
  • Comparing future with past dates?
  • How to determine if two dates are on the same day?

First, let’s check how you check in TypeScript.

How to Check Whether Two Dates Are Equal or Not in TypeScript?

When dealing with dates in TypeScript, you can compare date objects using <,>,==,>=,<= operators, but only with the Date.getTime() method.

In the example below:

  • We create two dates with the same values.
  • Date object comparison returns false.
  • Date.getTime() comparison returns true.

These examples compare if the dates and times are equal or not.

let date1 = new Date("2021-02-21");
let date2 = new Date("2021-02-21");
console.log(date1); // Sun Feb 21 2021 05:30:00 GMT+0530 (India Standard Time)
console.log(date2); // Sun Feb 21 2021 05:30:00 GMT+0530 (India Standard Time)

console.log(date1.getTime()); // 1613865600000
console.log(date2.getTime()); // 1613865600000
console.log(date1 === date2); // false
console.log(date1.getTime() === date2.getTime()); // true

How to Check Dates Without Time Are Equal in TypeScript?

In this example, we only compare the Date part and ignore the time value.

We can achieve this by writing a function to check if the year, month, and date are equal or not.

let date1 = new Date("2021-02-21");
let date2 = new Date("2021-02-21");
function isDatesEqual(date1, date2) {
  return (
    date1.getFullYear() === date2.getFullYear() &&
    date1.getMonth() === date2.getMonth() &&
    date1.getDate() === date2.getDate()
  );
}

console.log(isDatesEqual(date1, date2));

MomentJS: Comparing Dates Without Time

MomentJS provides the following methods to compare dates with or without a timestamp.

  • isSame: Checks whether two moment date objects are equal or not.
moment("2021-02-12").isSame("2021-02-12"); // true
moment("2021-02-12").isSame("2021-02-15"); // false
moment("2021-02-12").isSame("2021-02-10"); // false
  • isBefore: Checks if one date is before another date.
moment("2021-02-12").isBefore("2021-02-12"); // false
moment("2021-02-12").isBefore("2021-02-15"); // true
moment("2021-02-12").isBefore("2021-02-10"); // false
  • isAfter: Checks if one date is after another date
moment("2021-02-12").isAfter("2021-02-12"); // false
moment("2021-02-12").isAfter("2021-02-15"); // false
moment("2021-02-12").isAfter("2021-02-10"); // true

Conclusion

In conclusion, we’ve learned how to determine if dates are equal or not in TypeScript and Angular using both the equality operator and the MomentJS library.