Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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.

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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>
about 4 years ago · Santiago Trujillo Report

0

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);
}
about 4 years ago · Santiago Trujillo Report

0

A little extra but this is what I did based off the answer to compare expiration dates. Also, "expireDate" came as an object to I couldn't simply compare by two date objects.

.ts

today = new Date();

.html

<div *ngIf="(expireDate | date: 'yMMdd') > (today | date: 'yMMdd'); else template_Expired">
      {{ expireDate | date }}
</div>

<ng-template #template_Expired>
      <div class="expired">
        {{ expireDate | date }}
      </div>
</ng-template>

.css

.expired {
  font-weight: bold;
  color: red;
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!