1

Given a string in xsd:dateTime format I want to create a python datetime object. I especially need to be able to parse for example a string like this '2012-09-23T09:55:00', but also all other defined examples should be parsed correctly, and also use timezones.

Martin Flucka
  • 3,125
  • 5
  • 28
  • 44
  • possible duplicate of [How do I translate a ISO 8601 datetime string into a Python datetime object?](http://stackoverflow.com/questions/969285/how-do-i-translate-a-iso-8601-datetime-string-into-a-python-datetime-object) – Martijn Pieters Sep 11 '12 at 11:42

2 Answers2

6

Use the datetime.datetime.strptime class method to parse these:

dt = datetime.datetime.strptime(xsdDateTime, '%Y-%m-%dT%H:%M:%S')

Your example does not include a timezone however. If you really do need timezone support, best resort to the python-dateutil module:

from dateutil.parser import parse
dt = parse(xsdDateTime)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

i would also recommend the python-dateutil package. i am using version 2.7.3, that also has an isoparser. however, the isoparser doesn't parse time only strings, and i've filled an issue. use the parser module instead.

from dateutil.parser import parse
dt = parse(xsdDateTime)

hth, alex

alex
  • 651
  • 1
  • 9
  • 11