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

Time:01-06

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.

CodePudding user response:

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.

CodePudding user response:

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..

CodePudding user response:

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

  • Related