In the attribute table of my file geodatabase feature class I have a populated field called DateField of type date, and a text field called TextField.
I have not yet hit on a way to convert the dates in DateField to strings of mm/dd/yyyy
format in the TextField.
I think this answer to Converting ArcGIS date value to string reading in mm/dd/yyyy format using ArcPy? should have given me enough clues, but when I try the following in the Field Calculator it results in the error that follows.
Parser: Python
Pre-Logic Script Code:
from datetime import datetime
def date_only(datetimeVal):
formattedTime = datetime.strftime(datetimeVal, "%m/%d/%Y")
return formattedTime
TextField =
date_only( !DateField! )
The error is:
Messages
Executing: CalculateField CountriesFC TextField date_only( !DateField! ) PYTHON_9.3 "from datetime import datetime\n\ndef date_only(datetimeVal):\n formattedTime = datetime.strftime(datetimeVal, "%m/%d/%Y")\n return formattedTime"
Start Time: Mon Feb 02 08:45:07 2015
ERROR 000539: Error running expression: date_only( u"13/02/2015 2:47:58 PM" )
Traceback (most recent call last):
File "<expression>", line 1, in <module>
File "<string>", line 4, in date_only
TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'unicode'
Failed to execute (CalculateField).
Failed at Mon Feb 02 08:45:07 2015 (Elapsed Time: 0.03 seconds)
From the error I think that what I may be missing is the knowledge of how to convert the Unicode object that is being received by my date_only function into a datetime object.
Coincidentally, I noticed that there is currently a similar unanswered question on Stack Overflow: http://ift.tt/1KiHR4R
Aucun commentaire:
Enregistrer un commentaire