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.
Asked
Active
Viewed 4,005 times
1
-
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 Answers
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
-
No module named parsing edit: the modul is called parser not parsing but it seems to work now – Martin Flucka Sep 11 '12 at 11:44
-
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