Search code examples
angulardatedatetimedate-pipe

Angular HTML: compare string datetime to current datetime


How can I compare "2022-01-07 15:43:00" string variable with current datetime in *ngIf of angular html?

Please help and guide.


Solution

  • You can use a function in the ngIf directive, like *ngIf="showBasedOnDate(date)" create this function in your TS file and handle all logic there. Return either true or false to show the elements.

    <div *ngIf="showBasedOnDate(date)"> 
       I am shown
    </div>
    
    showBasedOnDate(date) {
     // here you can have as much logic as you would like, based on your needs. 
     return (date === new Date() );
    }