HOW TO
In ArcGIS Online, the DateDiff() function may not work properly when calculating the date difference between two fields, especially if the date fields are in different formats and are stored as text or string. The date difference between two text fields can be calculated using an Arcade expression. This method transforms qualitative temporal information into quantitative metrics, enabling more informed and strategic approaches to managing and interpreting time-sensitive data.
Note: The date difference between two text fields can only be calculated if the date format used in the text fields is in dd-mmmm-yyyy.
This article provides the workflow to calculate the date difference between two text fields in an attribute table using an Arcade expression in ArcGIS Online.
Note: Skip to Step 3 if the script is run on an existing field.
var months = { "January": "01", "February": "02", "March": "03", "April": "04", "May": "05", "June": "06", "July": "07", "August": "08", "September": "09", "October": "10", "November": "11", "December": "12" };
var <variable1> = $feature.<field_name1>; var <variable2> = $feature.<field_name2>;
var <variable3> = Split(<variable1>, " "); var <variable4> = Split(<variable2>, " ");
var dayNum = dateParts[0]; var monthNum = months[dateParts[1]]; var yearNum = dateParts[2];
var dayNum2 = dateParts2[0]; var monthNum2 = months[dateParts2[1]]; var yearNum2 = dateParts2[2];
var <variable5> = yearNum + "-" + monthNum + "-" + dayNum; var <variable6> = yearNum2 + "-" + monthNum2 + "-" + dayNum2; var dateDifference = DateDiff(<variable6>, <variable5>, "days"); return dateDifference;
Below is the full working code of the Arcade expression.
var months = {
"January": "01", "February": "02", "March": "03", "April": "04", "May": "05", "June": "06", "July": "07", "August": "08", "September": "09", "October": "10", "November": "11", "December": "12"
}; var startDate = $feature.StartDate; var endDate = $feature.EndDate; var dateParts = Split(startDate, " "); var dateParts2 = Split(endDate, " "); var dayNum = dateParts[0]; var monthNum = months[dateParts[1]]; var yearNum = dateParts[2]; var dayNum2 = dateParts2[0]; var monthNum2 = months[dateParts2[1]]; var yearNum2 = dateParts2[2]; var updatedStartDate = yearNum + "-" + monthNum + "-" + dayNum; var updatedEndDate = yearNum2 + "-" + monthNum2 + "-" + dayNum2; var dateDifference = DateDiff(updatedEndDate, updatedStartDate, "days"); return dateDifference;
Note: To calculate the difference between the two date fields in other units such as hours, minutes, or seconds, use the same expression and replace 'days' with the desired unit of measurement.
The image below displays the date difference in days between the two text fields calculated and populated in the new field.
Get help from ArcGIS experts
Download the Esri Support App