Using python-dateutil :
In [1]: import dateutil.parser as dparser In [18]: dparser.parse("monkey 2010-07-10 love banana",fuzzy=True) Out[18]: datetime.datetime(2010, 7, 10, 0, 0)
Invalid dates raise a ValueError :
In [19]: dparser.parse("monkey 2010-07-32 love banana",fuzzy=True)
It can recognize dates in many formats:
In [20]: dparser.parse("monkey 20/01/1980 love banana",fuzzy=True) Out[20]: datetime.datetime(1980, 1, 20, 0, 0)
Note that this makes an assumption if the date is ambiguous:
In [23]: dparser.parse("monkey 10/01/1980 love banana",fuzzy=True) Out[23]: datetime.datetime(1980, 10, 1, 0, 0)
But the way to handle ambiguous dates is configurable:
In [21]: dparser.parse("monkey 10/01/1980 love banana",fuzzy=True, dayfirst=True) Out[21]: datetime.datetime(1980, 1, 10, 0, 0)
unutbu Jul 18 '10 at 17:09 2010-07-18 17:09
source share