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

Typescripts Date compare: This tutorial explains how we can compare dates in Angular and typescript.

If we want to compare dates and see if they are the same, we must use logic for comparison.

If two objects are equal, which means the values or properties values are equal.

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

Date data is represented in typescript using the Date type.

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

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

As the Date object returns default values contains

  • week name
  • date
  • time
  • time zone

Let’s create two dates and compare using an 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 below questions

  • How to Compare date with time
  • How to compare dates without time?
  • Compare the future with past dates?
  • How to tell if two dates are on the same day?

First, check how you check in Typescripts.

How to check whether two dates are equal or not in typescript?

With dates, you can compare date objects using <,>,==,>=,<= operators with the Date.getTime() method only.

In the below example

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

These examples compare date and time 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, compare only the Date and ignore the time value.

So, This approach is to set We can write a function to check year date and month 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 compare date 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: 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: 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


To Sum up, Learned dates are equal or not in typescript and angular using the equality operator momentJS library.