I cant seem to get best way of handling date fields in ModelBuilder/ArcGIS environment. I created a very simple model in ModelBuilder that accepts a date object (that I used with 'Create Variable) (see first graphic). All I am trying to do is calculate that date object as a string formated in (YYYYmmdd) using the 'Calculate Variable' tool. I expected that since it is a date object that I could simply just use the ".strftime" method to format the date (see 2nd graphic). However I get an error message (3rd graphic) that seems to be implying that the object is an integer.
Can someone point me to how this should be done? I also tried constructing the date using using datetime.datetime.strptime() and then using strftime(), but that didnt work either
Solved! Go to Solution.
Thank you Lance - that worked!. Not sure I understand the "why" this is necessary, but Ill take it. I find dealing with dates when working in model builder/ArcGIS interfaces non-intuitive and more complicated than I would think they should be, but hopefully Ill catch on to the logic at some point
For some reason your function was seeing the "dte" parameter as an integer. Not knowing the entire setup of your model it is difficult to say why. If you look at your error message you can see the date was being passed Calc(6/15/2019), not an integer. By adding single or double quotes around the parameter "%Date%" it forces the parameter to a string. You can then convert that to a date and perform the needed formatting. I hope that helps as to the why.
For some reason your function was seeing the "dte" parameter as an integer. Not knowing the entire setup of your model it is difficult to say why.
I can explain this.
All model elements are strings. Even if they are dates or extents or feature classes, they are stored and passed from one tool to another using a string representation, with a data type ("Long", "Date", "Extent") to tell tools what data types they are so you can connect them to parameters. For example, if a tool parameter is an integer the model element's data type is what causes you to only see integer parameters in the pick list that appears when you connect the model element to the tool.
However in the Calculate Value tool you need to explicitly include % signs and quotes so Python sees the element value, and knows it is a string, as Python can't see those "handles" on elements, what is in the variable (string) is simply subtitued between the % symbols. So, in your case 6/15/2019 is being interpreted by Python a Python expression of as two integer divisions, left to right (result zero):
>>> 6/15/2019 0
To have Python see a string you need to use this expression so Python sees a string:
"%dte%" # Python sees "06/15/2019"
So, when we have feature class variables we pass to Calculate Value, it's important to use raw strings so the back slashes aren't interpreted as escape characters:
"%input feature class%" # Python sees "C:<tab>est" r"%input feature class%" # Python sees "C:\test"
Hope this is helpful!