datetime.datetime.today()

Michiel Overtoom motoom at xs4all.nl
Wed Sep 16 17:01:39 CEST 2015


This bit me once. I was comparing a date to a datetime, both representing the same day, so I expected them to be the same, but I was wrong. What I should have done was extracting the date of the datetime with the .date() function, and only then compare it to the other date:

>>> import datetime
>>> a = datetime.datetime.today()
>>> a
datetime.datetime(2015, 9, 16, 16, 57, 45, 150069)
>>> b = datetime.date.today()
>>> a == b
False
>>> a.date()
datetime.date(2015, 9, 16)
>>> a.date() == b
True

Greetings,



More information about the Python-list mailing list