I'd recommend installing the excellent dateutil module. (In Ubuntu/Debian, it is provided by the python-dateutil package).
dateutil can parse date strings into datetime objects: It can handle many different date formats without you having to lift a finger(*):
import dateutil.parser as dparser
date=dparser.parse("Mon May 7 1883 10:36:28")
print(date)
# 1883-05-07 10:36:28
date=dparser.parse("1685-3-21")
print(date)
# 1685-03-21 00:00:00
date=dparser.parse("12/17/1770")
print(date)
# 1770-12-17 00:00:00
Note that parse is interpretting "12/17/1770" as being of the form "MM/DD/YYYY". You can change this behavior using parse's dayfirst
and yearfirst
options. (See http://labix.org/python-dateutil)
print(type(date))
# <type 'datetime.datetime'>
datetime objects can be sorted easily:
dates=[dparser.parse("Mon May 7 1883 10:36:28"),dparser.parse("1685-3-21"),dparser.parse("12/17/1770"),]
dates.sort()
print(dates)
# [datetime.date(1685, 3, 21), datetime.date(1770, 12, 17), datetime.date(1833, 5, 7)]
If you prefer to not install the dateutil package, then you'll
have to roll your own method of converting date strings into datetime objects. This requires more work since you'll have to define the format. Below, '%Y-%m-%d' defines the YYYY-MM-DD format. See http://au2.php.net/strftime (or the man page of strftime) for more information on available format codes.
For example,
dates=[datetime.datetime.strptime(date_str,'%Y-%m-%d') for date_str in
('1883-5-7','1685-3-21','1770-12-17',)]
print([str(date) for date in dates])
# ['1883-05-07 00:00:00', '1685-03-21 00:00:00', '1770-12-17 00:00:00']
dates.sort()
print([str(date) for date in dates])
# ['1685-03-21 00:00:00', '1770-12-17 00:00:00', '1883-05-07 00:00:00']
To control the format when converting datetime objects back into printable strings, you can use the datetime.datetime.strftime() method.