0

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
10
  • If you wish to also ask about using the Python parser to do this then please use a separate question so that this one can be focused on Arcade.
    – PolyGeo
    Commented Jan 30 at 5:29
  • On the surface, there is nothing obvious that jumps out with the code itself, so providing sample or example data that recreates the issue would be helpful.
    – bixb0012
    Commented Jan 30 at 15:12
  • @bixb0012 I added some brief example data from the attribute table. I also elaborated upon how the 'Last_Inspection_Date' field was populated... not sure if that's helpful information or not.
    – user237830
    Commented Jan 30 at 16:18
  • Are you sure your dates are Date data type or are they string representations of dates, which would be Text? Dates and string representation of dates may need to be handled differently.
    – bixb0012
    Commented Jan 30 at 19:56
  • @bixb0012 The Data Type for Last_Inspection_Date is Date, with a custom format of yyyy-M-dd. The field I am trying to calculate "Days_Since_Last_Inspection" has a Data Type of Short and a Numeric number format up to 6 singnificant digits. I have tried adjusting date formats for the Last_Inspection_Date field, as well as adjusted Data Types for the Days_Since_Last_Inspection. I am baffled why this simple chunk of code does not return a result.
    – user237830
    Commented Jan 30 at 20:09

1 Answer 1

1

The error was within the field calculation I performed for the 'Last_Inspection_Date' field:

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

This code was including the local time within the date. I was thinking since the field was formatted for yyyy-M-d and since the code was formatted for %Y-%m-%d it would override the time series in my recycled code. However, from reading through technical details for datetime I realized the default format includes time, unless you specifically code for date only. The full code below generated a random date only. This then allowed me to use the original arcade code posted in the question above to calculate the difference between 'Last_Inspection_Date' and today's date in days.

def random_dates(start, end):
    import random
    from datetime import datetime
    frmt = '%Y-%m-%d'
    
    stime = datetime.strptime(start, frmt)
    etime = datetime.strptime(end, frmt)
    
    ptime = stime + random.random() * (etime - stime)
    random_date = ptime
    
    return random_date

Not the answer you're looking for? Browse other questions tagged or ask your own question.