Select to view content in your preferred language

Calculating the difference between two date fields using arcade

752
1
01-30-2024 11:57 AM
Labels (2)
JoshuaBarth
New Contributor

I can't find the error in my code. I am trying to calculate the difference between the current date and a previous inspection date. I want the difference to be calculated in days. I am using ArcGIS Pro's field calculator for this task and am using Arcade code.

Field Calculator verifies the "Expression is valid" but when I run the code in Field Calculator for the 'Days_Since_Last_Inspection' field, the attribute table is not populated with any values and there are no errors... but the field still reads <Null> for each attribute in the 'Days_Since_Last_Inspection' field.

var startDate=Date($feature.Last_Inspection_Date)
var endDate=Now()
var result=DateDiff(endDate,startDate,'days')
return result

I have also tried adding a separate field to host todays date value. Therefore, the alternate code I have tried is:

var startDate=Date($feature.Last_Inspection_Date)
var endDate=Date($feature.Today_Date)
var result=DateDiff(endDate,startDate,'days')
return result

And field calculator is still not producing a calculated result between the two dates.

I am using ArcGIS Pro version 2.9.5, the 'Last_Inspection_Date' field is a date field formatted as yyyy-M-d, and the 'Days_Since_Last_Inspection' field is formatted as numeric with a Short data type. I have tried creating a text field thinking it might be the data type of the field preventing the calculation but that has not worked either. I have tried the similar questions asked on the topic and can't figure out why the code runs but does not produce the desired output.

Additional Information: I populated the 'Last_Inspection_Date' field with random dates using the following arcade code:

random_dates("2010-1-1", "2024-1-26"

 

1 Reply