Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello!

I am trying to compare 2 dates to be able to display the data in a table.
I am doing the following:
${this.dateToCompare.getTime()} > ${row.CreateDate.getTime()}

The problem is, CreateDate has a null value on some rows so the above is not working.

Is it possible to tell ng-if to not run it if null and to skip that row?
What is the best way to be able to fix this problem and be able to run the getTime() on it?
Thank you

What I have tried:

<div ng-if="${row.CreateDate!==null} && ${this.dateToCompare.getTime()} > ${row.CreateDate}"

I need to check first if the row is null or not, but even when it is null, it is continuing after the && and when it hits the dateToCompare.getTime, it is throwing a syntax error because I am calling getTime on a null value.
Posted
Updated 23-Sep-21 1:29am

1 solution

You can create a function that you pass in the row and date to compare, the function returns true or false.

JavaScript
function compareDates(date1, date2) {
if(date1 === null || date2 === null) {
return false;
} 

return date1 > date2;

}


You then just use your ng-if calling the function:

HTML
<div ng-if="compareDates(this.dateToCompare, row.CreateDate)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900