Search code examples
pythondatedate-comparison

How to compare dates in python and find the greater one


I want to compare 2 date and predict a label true if date 1 greater than date 2 and predict false date 1 less than date 2.
I have trained the model but model is predicting wrong for near by dates that is if 13-01-2020 and 14-01-2020 is given it will predict true but the right answer is false.


Solution

  • Try this:

    import datetime
    StartDate = "13-01-2020"
    EndDate = "14-01-2020"
    res = datetime.datetime.strptime(StartDate, '%d-%m-%Y')
    res2 = datetime.datetime.strptime(EndDate, '%d-%m-%Y')
    if res>res2:
        print(StartDate)
    elif res<res2:
        print(EndDate)
    

    Convert string into datetime format and then compare it.