Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best the best way to compare dates using ngIf in angular 2?

Tags:

angular

I am trying to compare dates in my app using ngIf, but I haven't been able to get it working properly so far.

<div *ngIf="(todaysdate | date:'MMM dd' == duedate | date:'MMM dd')">
List
</div>

I want to compare them using the date pipe because times on the full date string are different I only want to compare the dates. Any suggestions on how to properly do this are much appreciated.

like image 867
DN0300 Avatar asked Apr 11 '17 15:04

DN0300


2 Answers

Compare works fine when you format your dates properly (eg. 20191224 for christmas). So this should work

<div *ngIf="(todaysdate | date:'yMMdd' == duedate | date:'yMMdd')">
    List
</div>
like image 61
Antti A Avatar answered Sep 21 '22 21:09

Antti A


The quickest solution is to this.todaysdate.setHours(0, 0, 0, 0); which you would do on your date prior in your constructor.

But you can also send it to and expression on the component *ngIf="compareDates()"

compareDates(){
   return this.todaysdate.setHours(0, 0, 0, 0) == this.duedate.setHours(0, 0, 0, 0);
}
like image 29
Dylan Avatar answered Sep 22 '22 21:09

Dylan