Python date comparison not giving correct result, am I missing something super simple?

As part of a larger project I need to check if due dates have passed to mark tasks overdue, I’m relatively new at this so I could 100% be overlooking something glaringly obvious, but my date comparison isn’t outputting the correct result. what am I missing?

Thank you in advance! 🙂

so this is my test attempt..

import datetime

date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y').strftime('%d.%m.%Y')
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y').strftime('%d.%m.%Y')
today = datetime.date.today().strftime('%d.%m.%Y')

if date1 < today:
    print("OVERDUE")
else: 
    print("not overdue")
    
if date2 < today:
    print("OVERDUE")
else:
    print("not overdue")

I intentionally set date1 to be overdue just to make sure it worked but despite that I still get the ‘not overdue’ option for both date1 and date2.

New contributor

epv is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

0

You should compare in the format [%Y%m%d]

Code:-

import datetime

date1 = datetime.datetime.strptime("2022.12.25", '%Y.%m.%d').strftime('%Y.%m.%d')
date2 = datetime.datetime.strptime("2025.12.25", '%Y.%m.%d').strftime('%Y.%m.%d')
today = datetime.date.today().strftime('%Y.%m.%d')

if date1 < today:
    print("OVERDUE")
else: 
    print("not overdue")
    
if date2 < today:
    print("OVERDUE")
else:
    print("not overdue")

Output:

OVERDUE
not overdue

Reason you should do like this.. when you are comparing in date,month,year format it is not guarantees that date will be greater always if the year is greater. example 25.12.2022 06.01.2023 when you comparing date 25 will be greater than 06 hence the output it gives 25.12.2022>06.01.2023 which is not correct.! that’s why you should compare in year than month than date format..

Your issue is that, because you called strftime on your datetime and date objets, date1, date2 and today aren’t date objects, they’re strings. So, the string “25.12.2022” is not less than “06.01.2023” (ie. date1 < today is False). The obvious solution here is to remove the call to strftime but then you’ll have the issue of comparing dates and datetimes. To fix that, you can do either of the following:

As Dates

Convert all your objects to dates, and compare them that way. This just means calling date() on date1 and date2, like so:

import datetime

date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y').date()
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y').date()
today = datetime.date.today()

As Datetimes

Convert all your objects to datetimes. This just means replacing your call to today with a call to now, like so:

import datetime

date1 = datetime.datetime.strptime("25.12.2022", '%d.%m.%Y')
date2 = datetime.datetime.strptime("25.12.2025", '%d.%m.%Y')
today = datetime.date.now()

In either case, the comparison will work as expected.

You could try something like this. The below comparison is between datetime data types and not Strings:

from datetime import datetime

date1 = datetime.strptime("25.12.2022", '%d.%m.%Y') <-- removed the extra strptime
date2 = datetime.strptime("25.12.2025", '%d.%m.%Y') <-- removed the extra strptime
today = datetime.today() <-- removed the strptime

print(type(date1))
print(date2)
print(type(today))

if date1 < today:
    print("OVERDUE")
else:
    print("not overdue")

if date2 < today:
    print("OVERDUE")
else:
    print("not overdue")

Output:

<class ‘datetime.datetime’>

2025-12-25 00:00:00

‘datetime.datetime’>

OVERDUE

not overdue

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật