HOW TO
ArcGIS Arcade is an expression language that can be used to configure pop-ups in Portal for ArcGIS Map Viewer. The Arcade Date functions provide methods for creating date objects and acquiring various properties of the objects. For example, the DateAdd() and DateDiff() functions are used to adjust the date based on a specified interval, and the Now() function is used to obtain the current local time of the client.
This article provides the steps to calculate the difference between two date fields in the pop-up in Portal for ArcGIS Map Viewer. In this example, the displayed number of days before the last fire hydrant inspection in the pop-up is obtained by referencing the field values in the Date Inspected field (as seen in the image below) and the current local time of the client.
var <variableName> = Now() var <variableName1> = $feature.<date_fieldName>;
var <variableName2> = Floor(DateDiff(<variableName>, <variableName1>, 'years'));
var <variableName3> = DateAdd(<variableName1>, <variableName2>, 'years');
var <variableName4> = DateDiff(<variableName>, <variableName3>, 'days');
if (IsEmpty($feature.<date_fieldName>)){ return "<textValue>"; } else if (<variableName2> >= <numberValue>) { return "<textValue1> " + <variableName2> + " <textValue2> " + Floor(<variableName4> , <numberValue1>) + " <textValue3>"; } else if (<variableName2> == <numberValue2>) { return "<textValue4> " + Floor(<variableName4> , <numberValue1>) + " <textValue3>"; } else if (<variableName2> == <numberValue1> && <variableName4> >= <numberValue>) { return "<textValue1> " + Floor(<variableName4> , <numberValue1>) + " <textValue3>"; } else if (<variableName2> == <numberValue1> && <variableName4> >= <numberValue2>) { return "<textValue1> " + Floor(<variableName4> , <numberValue1>) + " <textValue5>"; } else if (<variableName2> == <numberValue1> && <variableName4> < <numberValue2>) { return "<textValue6>"; }
The following code block demonstrates the full working Arcade script.
var timeNow = Now(); var survey = $feature.DATEINSPECTED; var y = Floor(DateDiff(timeNow, survey, 'years')); var date1a = DateAdd(survey, y, 'years'); var d = DateDiff(timeNow, date1a, 'days'); if (IsEmpty($feature.DATEINSPECTED)){ return "-"; } else if (y >= 2) { return "Last Inspection: " + y + " years and " + Floor(d , 0) + " days ago."; } else if (y == 1) { return "Last Inspection: 1 year and " + Floor(d , 0) + " days ago."; } else if (y == 0 && d >= 2) { return "Last Inspection: " + Floor(d , 0) + " days ago."; } else if (y == 0 && d >= 1) { return "Last Inspection: " + Floor(d , 0) + " day ago."; } else if (y == 0 && d < 1) { return "Last Inspection: today."; }
The image below shows the newly added Last Inspection (days) field in the pop-up using ArcGIS Arcade to provide a count of the days or years since a hydrant was last inspected.
Get help from ArcGIS experts
Download the Esri Support App