Incorporating Angular 7, my goal is to exhibit elements in the component HTML only if a certain condition is met: namely, if the provided time is ahead of the current time.
After several attempts, I came up with the following logic:
checkTimeDifference(time, date)
{
const currentDate = new Date()
const slotDate = new Date(`${date} ${time}`);
const difference = Math.abs(Math.abs(currentDate.getTime() - slotDate.getTime()) / 3600000)
// If the difference is not negative
if(difference) {
return false;
}
else {
return true;
}
}
For implementing this in HTML:
<span *ngIf="checkTimeDifference(result.endtime, result.date)"> opened </span>
Update:
The elements are being showcased using *ngFor loop, thus calling getTimeDiff in ngOnInit() is not feasible.
<div *ngFor="let result of results">
<span *ngIf="checkTimeDifference(result.endtime, result.date)"> opened </span>
</div>
However, an error is encountered stating:
ViewAppointmentsComponent.html:30 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'null: 6.0732225'. Current value: 'null: 6.0732252777777775'.