[2.5.1] Comparing dates?

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Feb 2 04:06:02 EST 2009

Gilles Ganault <nospam at nospam.com> writes:

> 	I have data in an SQL database where one column contains a
> date formated as DD/MM/YYYYY.
> I need to select all rows where the date is before, say Feb 1st
> 2009, ie. 01/02/2009.

The Python data types for date and time are in the ‘datetime’ module
<URL:http://www.python.org/doc/2.6/library/datetime>. Create a
‘datetime’ object for each value you want, then compare them.

To create a ‘datetime’ value from a string, use ‘datetime.strptime’
(new in Python 2.5).

 \                            “Holy knit one purl two, Batman!” —Robin |
  `\                                                                   |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list