Skip to content Skip to sidebar Skip to footer

Converting Legacy String Dates To Dates

We have some legacy string dates that I need to convert to actual dates that can be used to perform some date logic. Converting to a date object isn't a problem if I knew what the

Solution 1:

Using dateutil:

In [25]: import dateutil.parser as parser

In [26]: parser.parse('25 December 2010')
Out[26]: datetime.datetime(2010, 12, 25, 0, 0)

In [27]: parser.parse('Dec 25, 2010')
Out[27]: datetime.datetime(2010, 12, 25, 0, 0)

Solution 2:

The typical approach is to define a list of formats (strptime formats, specifically), and try them in turn, until one works. As an example, see this recipe:

http://code.activestate.com/recipes/577135-parse-a-datetime-string-to-a-datetime-instance/

strptime accepts quite a number of formats. If you can enumerate all the possibilities you'll see, you should be able to hack together a variant of that recipe that'll do what you want.

Post a Comment for "Converting Legacy String Dates To Dates"