ngIf today?


#1

In the view I need to check if today is the same as the post date. I got this now which doesn’t work.

       <div *ngIf="post?.date === now; then isToday else notToday"></div>
        <ng-template #isToday>
            <span class="date">{{ post?.date | date:'EEEE d MMMM' }}</span>
            Today
        </ng-template>
        <ng-template #notToday>
            <span class="date">{{ post?.date | date:'EEEE d MMMM' }}</span>
            not today
        </ng-template>

Where am I going wrong?

What I tried:

today instead of now.
== instead of ===

Adding:

        this.today = new Date();

To .ts.


#2

JavaScript’s Date objects are terrible. Try using ISO8601 strings instead.


#3

How does your post date look like?


#4

Ah I found the problem. The JS date object uses a dateTime so since the time was different it would display ‘not today’.

I used formatting in the ngIf to only compare the date:

<div *ngIf="(post?.date | date:'EEEE d MMMM') == (today | date:'EEEE d MMMM'); then isToday else notToday"></div>

The above works on Ionic serve. But now I have a slightly different problem.

If i do this:

  {{ post?.date | date:'EEEE d MMMM'}}

  {{ today | date:'EEEE d MMMM'}}

It does display when using Ionic serve but it doesn’t when emulating on iOS.

So the if gives false on my iOS emulator.


#5

It’s always good to check dates in Safari, since browsers handle dates very differently. IMO dates are always a pain in the ass. I’m pretty sure it won’t work their either, so that’s a little bit easier to debug.


#6

Hmm after relaunching the iOS emulator it suddenly does display the dates. So I guess that solution does work. And now after implementing it in another view it does work in 1 and not in the other.


#7

DatePipe considered harmful.