{

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


Typescript compare two dates example 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 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

  • week name
  • date
  • time
  • time zone

Let’s create two dates and compare using 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 future with past dates?
  • How to tell if two dates are on the 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 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

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 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 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

Conclusion

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

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.