Without explaining all my reasoning for why we need to do this, I need to take a field that our GPS units create called "CreationDateandTime" and use the values in there to create a new "UID" field that is just a text field containing the 12hr time as a string. I will also take the colons out, but that isn't my issue right now.

The values in the "CreationDateandTime" field are strings formatted like this example:


I need my final output (in this case) to be "152" or "0152".

I am in ArcGIS Pro, Python 3.X

So if I just do this in Calculate Field:


it gets the 24hr time as a string.(13:52 in this case) But I need to convert it to 12 hr time and then back to a string.

Here is what I thought should work:

from datetime import datetime

timeStr = !CreationDateandTime![11:16]
timeDT = timeStr.datetime.time(12, 59)
UID = str(timeDT)

def UIDmaker():

The error comes in at

timeStr = !CreationDateandTime![11:16]

It says that it is invalid syntax.

Can I not call a field in a variable definition and, if that is the case, would something like this work?

from datetime import datetime

cur = arcpy.UpdateCursor(r'C:\Users\path_to_gdb\featureClass_name')

for row in cur:
    UID = row.getValue('CreationDateandTime')

timeStr = UID[11:16]
timeDT = timeStr.datetime.time(12, 59)
UIDnew = str(timeDT)

def UIDmaker():

I am trying to accomplish this with a script that I can save and deploy as a ".cal" file for any featureClass from the Claculate Field Tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.