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.
Example Data:
Last Inspection Date | Days Since Inspection |
---|---|
2021-8-1 | <Null> |
2019-3-30 | <Null> |
2013-7-30 | <Null> |
Additional Information: I populated the 'Last_Inspection_Date' field with random dates using the following python code:
random_dates("2010-1-1", "2024-1-26")
def random_dates(start, end):
import random
from datetime import datetime
frmt = '%Y-%m-%d'
stime = time.mktime(time.strptime(start, frmt))
etime = time.mktime(time.strptime(end, frmt))
ptime = stime + random.random() * (etime - stime)
random_date = datetime.fromtimestamp(time.mktime(time.localtime(ptime)))
return random_date