Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

1.4K
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda