4

I have a Date column called readings_measurement_tstamp stored in my feature class. I would like to add a String column to this feature class and Field Calculate it to be be just the %H:%M:%S component of readings_measurement_tstamp. After adding the new column I calculated it according to the following expression: datetime.datetime.strftime( !readings_measurement_tstamp! ,'%H:%M:%S')

I was under the impression that Date columns can be referenced like python datetime objects. Therefore, I was very confused when running the field calculator with this expression gave the following error: Descriptor 'strftime' requires a 'datetime.date' object but received an 'str'

The error implies to me that readings_measurement_tstamp is being stored as text, not as a Date. Any ideas what I need to do in order to be able to access readings_measurement_tstamp as a datetime object? Thanks!

1 Answer 1

4

Try this code. !UT! is a date field in my FeatureClass storing date and time in the format of "22/11/2012 11:23:30"

import datetime
from dateutil import parser

def convert(ut):
  dateobject = parser.parse(ut)
  return datetime.datetime.strftime(dateobject,'%H:%M:%S')

The Expression you apply to your string field in the Field Calculator would be

convert(!UT!)
0

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.