I am not able to fetch accurate date based on condition it is giving me wring output

49 Views Asked by At

i am not able to fetch accurate date based on condition it is giving me wring output. i want to fetch the date which is before 01/01/2010 but it is giving true even date is less than 01/01.2010

https://i.stack.imgur.com/zLlV6.png

1

There are 1 best solutions below

0
On

You should convert string to datetime to compare.

from datetime import datetime

#date_time_str = '1-1-2010'

def str_to_date(date_time_str):
    return datetime.strptime(date_time_str, '%d-%m-%Y') # y - two digit(19), Y - four digit(2019)

def compare_date_from_str(str1, str2):
    return str_to_date(str1) > str_to_date(str2)

print(compare_date_from_str('1-1-2010', '11-12-2009'))