2

I have a text field within a FGBD that contains a date/time and I need only the time as a string.

'yyyymmdd HH:mm:ss'

And I need:

'HH:mm:ss'

I've tried .rsplit but I'm not using it correctly. I thought this would split where the space is located (between the date and time):

arcpy.Calculatefield_management(fc,"time",!DATE!.rsplit())

But this returns the whole string

3
arcpy.CalculateField_management(fc, "time", "!DATE!.split(' ')[1]", "PYTHON")

This uses Python to split the string at the space (which returns a list containing the date as a string and the time as a string), and populates the field with the second item in the list, which is the time.

2

You are close, but the (r)split will create a list. Therefore you need to get the element from the list (-1 for the last one)

arcpy.Calculatefield_management(fc,"time",!DATE!.rsplit()[-1])
1

You can try this code instead: arcpy.Calculatefield_management(fc,"time",!DATE![9:len(!DATE!)])

Len gets the length of the string the square brackets are saying, "Return the 9th character of the string (in date) to the last character of the string"

Your Answer

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

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