Calculating two date difference in *ngFor loop

I have an array of objects where I am looping through to display data in the HTML using *ngFor… each object also contains 2 dates , start and end.

How can I calculate the difference and display it in months / years ?

what is the best way in this case to achieve this, thanx

HTML:

<ion-item *ngFor="let event of events">
        {{calcuateDif(event)}}
</ion-item>

TS:

calculateDif(event)
{
let diff = Math.abs(event.date1 - event.date2);
return new Date(diff);
}
2 Likes

hi, thanx for your answer , just one more thing about the loop
*ngFor="let event of events | slice:0:3; let i=index
I am only taking the first 3 … is there a way just to pass those 3 … Im just concerned about the load and maybe performance, not sure if it really causes any!

*ngFor="let event of events.slice(0,3)"

1 Like