comparing datetime with date
peter at engcorp.com
Tue Sep 14 22:24:02 CEST 2004
Diez B. Roggisch wrote:
> Donnal Walter wrote:
>>How can these two objects be considered equal? Is there a *general* way
>>to test for date != datetime as well as 4.5 != 4.6?
> Try making a datetime-object from your date, with zeros for the time part -
> then you'll get what you want.
While trying to provide my first answer, I thought of suggesting
this, but there doesn't appear to be a simple way of doing it.
It looked like the following was the best I could do (where 'x'
is a datetime.date object):
z = datetime.datetime(x.year, x.month, x.day)
I just found timetuple(), which means this could be written
in the awful form:
z = datetime.datetime(*x.timetuple()[:3])
but that's hardly better, and definitely more inscrutable.
To answer Donnal's question above, however, it appears this
might be a reasonable approach:
>>> x.timetuple() == y.timetuple()
Not sure there's a better approach readily available.
More information about the Python-list