I have a string variable (fileDateString) with the date in this format: 20150404

In my feature class there is a text field with time in twenty-four hour UTC format. There is also 2 leading spaces before the time. E.g. " 1300"

I have added a Date field to my feature class and I would like to calculate a date/time. I also want to be able to subtract 6 hours from the date/time (convert UTC to CST) for the final result.

I thought about first creating a text field with the date and time, then converting that to date but am wondering if there is an easier way?

expression = "\"" + str(fileDateString) + "\"" + "!DateUTC!"

1 Answer 1


Based on the requirements you described, the following Python expression should do what you're looking for:

"datetime.datetime.strptime({} + !date1!.strip(), '%Y%m%d%H%M') - datetime.timedelta(hours=6)".format(repr(fileDateString))

Note: I'm assuming months before days in your string. If not switch the %m and %d.

  • I changed my answer to use repr() instead of `` for Python 2/3 compatibility.
    – DWynne
    Commented Apr 6, 2015 at 20:17

Your Answer

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

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