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


Sometimes, We want to compare the dates if it is same, We need to display

This tutorials explains how we can compare dates in Angular

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

In case of Date object, it contains date and time in milli seconds.

In typescript date data is referenced with Date type.

Let’s create an Date object and prints the date using console.log

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

As Date object returns default values contains

  • weekname
  • date
  • time
  • timezone

Let’s create two dates and compare using == 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 return false.

This post answers below questions

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

First check how do you check in Typescripts?

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

With dates, you can compare date object using <,>,==,>=,<= operators with Date.getTime method only.

In the below example

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

This examples compared 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 date without time are equal in Typescript?

In this example, compare only Date and ignoring 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 following methods to compare dates with or without timestamp.

  • isSame: checks 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
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

How to convert json to/from  object in angular|Typescript 

Angular ngFor index - How to get current last even and odd index?

How to Format array of dates in typescript Angular example

In this post, you learn writing comments in Angular and typescript. You can also write single multiline and documentation comments in Typescript. This includes declare comments in Angular typescript and template HTML components.

Typescript Singleton pattern Implementation with example

Learn Typescript compiler option,tsconfig.json file

Difference between ngx-bootstrap and ng-bootstrap