2

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.

enter image description here

I have not yet hit on a way to convert the dates in DateField to strings of dd/mm/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, "%d/%m/%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.date object.

Coincidentally, I noticed that there is currently a similar unanswered question on Stack Overflow.

4
  • Do different databases store dates in different formats? I have a recollection of there being a difference between shape/personal GDB/file GDB (and SDE) date storage. If you are receiving a unicode string u"13/02/2015 2:47:58 PM" consider converting to ASCII string stackoverflow.com/questions/2365411/… to suit the function then string.spit to separate values. Feb 1, 2015 at 23:32
  • @MichaelMiles-Stimson - there's a page on Fundamentals of date fields that has been quite helpful but I have just not yet cracked how to do this. I think the Stack Overflow Q&A you linked to appears more complicated than I suspect this needs to be but I will re-assess that if I stay stuck.
    – PolyGeo
    Feb 1, 2015 at 23:44
  • The error message is saying that Arc has converted the DATETIME object into a string for you; I assume that it did that because you're calculating into a string field. Treat the date now as a unicode string and not a date object. Feb 1, 2015 at 23:47
  • 1
    @MichaelMiles-Stimson You're right, it must be that the Field Calculator is doing that datetime to string object conversion. While in the answer by blah238 on the other question reading the dates using a cursor brought them in raw. By using .split(" ")[0] I can now get at what I need - thanks!
    – PolyGeo
    Feb 2, 2015 at 0:00

1 Answer 1

3

With the help of @MichaelMiles-Stimson the Pre-Logic Script Code I ended up using was:

def date_only(datetimeVal):
      datePart = datetimeVal.split(" ")[0]
      return datePart

The reason I needed to do it this way rather than the precise method used by @blah238 in the answer to the other question was that the Field Calculator performs a datetime to string object conversion. In contrast using cursors can access the datetime objects in their raw state.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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