I've got a python string like this "2012/04/08 13:31:00 UTC" How can I convert it to a datetime object?
2 Answers
Use datetime.strptime.
from datetime import datetime datetime.strptime("2012/04/08 13:31:00 UTC", "%Y/%m/%d %H:%M:%S %Z") returns the datetime object:
datetime.datetime(2012, 4, 8, 13, 31) Comments
How about this?
from dateutil.parser import parse parse('2012/04/08 13:31:00 UTC') gives
datetime.datetime(2012, 4, 8, 13, 31, tzinfo=tzutc())