The dateutil module has a date parser which can parse date strings in many formats.
For example,
In [13]: import dateutil.parser as parser
In [14]: parser.parse("19970902T090000")
Out[14]: datetime.datetime(1997, 9, 2, 9, 0)
In [15]: import datetime as dt
In [16]: now = dt.datetime.now()
In [17]: now.isoformat()
Out[18]: '2012-11-06T15:08:51.393631'
In [19]: parser.parse('2012-11-06T15:08:51.393631')
Out[19]: datetime.datetime(2012, 11, 6, 15, 8, 51, 393631)
In [20]: parser.parse('November 6, 2012')
Out[20]: datetime.datetime(2012, 11, 6, 0, 0)
Note that some datetime strings can be ambiguous: 10-09-2003
could mean October 9 or September 10, for example. dateutil
has parameters like dayfirst
and yearfirst
to handle this:
In [21]: parser.parse("10-09-2003")
Out[21]: datetime.datetime(2003, 10, 9, 0, 0)
In [22]: parser.parse("10-09-2003", dayfirst = True)
Out[22]: datetime.datetime(2003, 9, 10, 0, 0)
In [23]: parser.parse("10-09-03", yearfirst = True)
Out[23]: datetime.datetime(2010, 9, 3, 0, 0)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…