HOW TO

Calculate the difference between two date fields in a pop-up using Arcade in Portal for ArcGIS Map Viewer

Last Published: January 30, 2024

Summary

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.

The pop-up highlighting the Date Inspected field to be referenced in calculating the number of days since the last fire hydrant inspection

Procedure

  1. Open the map in Portal for ArcGIS Map Viewer. Click Layers on the Contents (dark) toolbar, and select the layer with the pop-up for editing. In this example, the Hydrant Painting for Collector layer is selected.
The Map Viewer Contents (dark) toolbar with the Layers tab
  1. Click Pop-ups on the Settings (light) toolbar and click Options > Attribute expressions.
The Map Viewer Pop-ups pane used to navigate to the Map Viewer Pop-up expressions pane
  1. Click + Add expression to open the Arcade expression editor window.
  2. In the editor window, specify a name for the new expression as required.
  3. In the Expression dialog box, insert the Arcade expression below.
  1. Define the dates to be referenced.
var <variableName> = Now()
var <variableName1> = $feature.<date_fieldName>;
  1. Calculate the date difference in years.
var <variableName2> = Floor(DateDiff(<variableName>, <variableName1>, 'years'));
  1. Create a new date by adding the years to the start date.
var <variableName3> = DateAdd(<variableName1>, <variableName2>, 'years');
  1. Calculate the number of days between those dates.
var <variableName4> = DateDiff(<variableName>, <variableName3>, 'days');
  1. Define the constraints for the field values to be displayed in the pop-up.
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.";
}
  1. Click Test to view a preview of the expression result. If the required result is achieved, click OK.
Specifying the Arcade expression in the Portal for ArcGIS Map Viewer editor window
  1. Click the Previous button above the newly created Arcade expression in the Attribute expressions pane.
The Portal for ArcGIS Map Viewer Pop-up expressions pane with the Previous icon above the newly created expression
  1. Click Fields list to expand the list of fields displayed in the pop-ups and click Select fields.
  2. Check the expression field under the Expressions section and click Done.

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.

The end result of the pop-up display in Portal for ArcGIS Map Viewer

Article ID: 000028205

Software:
  • ArcGIS Online
  • Portal for ArcGIS

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options