Search code examples
pythondatedatetime

How to compare dates only (and not the time) in python


I have 2 datetime objects. One only has the date and the other one has date & time. I want to compare the dates only (and not the time). This is what I have:

d2=datetime.date(d1.year,d1.month,d1.day)
print d2 == d1.date

It prints out false. Any idea why?

Thank you!


Solution

  • d1.date() == d2.date()
    

    From the Python doc:

    datetime.date() Return date object with same year, month and day.