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.
Solved! Go to Solution.
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...
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.
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
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.
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
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.
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
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...