Select to view content in your preferred language

Calculating elapsed time as an attribute field

214
7
Jump to solution
3 weeks ago
dsinha
by
Regular Contributor

Hello,

I need to calculate the number of days a permit has been open since it was issued. I am using this python code:

datetime.datetime.now().day - !permitissdate!

The permitissdate is a date field, and the field to which the calculation is being written (timeopen) is a short field.

I am getting the following error:

File "<expression>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'int' and 'str'

Could someone help me identify the problem?

Thanks in advance!

Deb.

0 Kudos
1 Solution

Accepted Solutions
dsinha
by
Regular Contributor

Wanted to report back that Esri Technical support helped me find a resolution to this issue. The solution has two steps:

1) Calculating the number of elapsed days

Used an Arcade expression:

var startDate = Date($feature.FieldName)
var endDate = Date()
var result = DateDiff(endDate, startDate, 'Day')
return result

In the Calculate Field tool select Arcade as the expression type. In the first line, replace FieldName with the field name.

2) Updating the number of elapsed days daily

Schedule the Calculate Field geoprocessing tool in ArcGIS Pro as a scheduled task with a daily frequency. Details here: https://pro.arcgis.com/en/pro-app/latest/help/analysis/geoprocessing/basics/schedule-geoprocessing-t...

View solution in original post

0 Kudos
7 Replies
DavidSolari
MVP Regular Contributor

Your expression is calculating the difference between the current day of the month and the permit date, which probably isn't what you want. Try(datetime.datetime.now() - !permitissdate!).days instead. On top of that, you'll want utcnow instead of now if your permit dates are stored in UTC instead of a consistent local time. Either way, this ran with no issue in Pro 3.1. If you're using ArcMap or a much older version of Pro the Field Calculator brings dates in as formatted strings instead of datetime objects. To fix this you'll need to parse that string in whatever format is spat out using the datetime.datetime.strptime method.

0 Kudos
dsinha
by
Regular Contributor

Thanks @DavidSolari 

I just tried that and I got this error:

File "<expression>", line 1
(datetime.datetime.now() - u"11/23/2022").days
^
SyntaxError: invalid non-printable character U+00A0

The date the error is referring to is actually 1/23/2024, and not 11/23/2024.

I am using ArcGIS Pro 3.2.1

0 Kudos
DavidSolari
MVP Regular Contributor

Odd, I guess a nonstandard character snuck into my answer. Try typing it out by hand and it should work, or at least you'll get a better error message. I wouldn't trust the date it reports until all the other errors are solved.

0 Kudos
dsinha
by
Regular Contributor

I typed it out and the error message changed to:

File "<expression>", line 1, in <module>
TypeError: unsupported operand type(s) for -: 'datetime.datetime' and 'str'

Thanks @DavidSolari 

0 Kudos
DavidSolari
MVP Regular Contributor

Looks like you're getting the date in a string format, probably month/day/year. Take a look at the strptime method I mentioned above, you can parse the date string that way.

0 Kudos
dsinha
by
Regular Contributor

I am sorry @DavidSolari, I am not well-versed in Python.

I tried to figure out the issue and this is as far as I got:

datetime.datetime.strptime(%m/%d/%Y) - !permitissdate!).days

The error is:

File "<expression>", line 1
datetime.datetime.strptime(%m/%d/%Y) - u"1/23/2024").days
                                                  ^
SyntaxError: invalid syntax

0 Kudos
dsinha
by
Regular Contributor

Wanted to report back that Esri Technical support helped me find a resolution to this issue. The solution has two steps:

1) Calculating the number of elapsed days

Used an Arcade expression:

var startDate = Date($feature.FieldName)
var endDate = Date()
var result = DateDiff(endDate, startDate, 'Day')
return result

In the Calculate Field tool select Arcade as the expression type. In the first line, replace FieldName with the field name.

2) Updating the number of elapsed days daily

Schedule the Calculate Field geoprocessing tool in ArcGIS Pro as a scheduled task with a daily frequency. Details here: https://pro.arcgis.com/en/pro-app/latest/help/analysis/geoprocessing/basics/schedule-geoprocessing-t...

0 Kudos