close

[Solved] TypeError: ‘>’ not supported between instances of ‘datetime.datetime’ and ‘str’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: ‘>’ not supported between instances of ‘datetime.datetime’ and ‘str’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: ‘>’ not supported between instances of ‘datetime.datetime’ and ‘str’ Error Occurs?

Today I get the following error TypeError: ‘>’ not supported between instances of ‘datetime.datetime’ and ‘str’ in python.

How To Solve TypeError: ‘>’ not supported between instances of ‘datetime.datetime’ and ‘str’ Error ?

  1. How To Solve TypeError: '>' not supported between instances of 'datetime.datetime' and 'str' Error ?

    To Solve TypeError: '>' not supported between instances of 'datetime.datetime' and 'str' Error You can use dateutil package and just convert your date string date to `datetime object and then check the condition with

  2. TypeError: '>' not supported between instances of 'datetime.datetime' and 'str'

    To Solve TypeError: '>' not supported between instances of 'datetime.datetime' and 'str' Error You can use dateutil package and just convert your date string date to `datetime object and then check the condition with

Solution 1

You have to use strptime to convert a string into a date.

The comparaison operator only applies between datetime.

date = datetime.strptime('2018-11-10 10:55:31', '%Y-%m-%d %H:%M:%S')

then can you do

if past > date :
   print("This is older than 90 days")

Solution 2

You can use dateutil package and just convert your date string date to `datetime object and then check the condition with :

from dateutil import parser
past = datetime.now() - timedelta(days=90)
new_date = parser.parse("2018-11-10 10:55:31+00:00")
if past > new_date :
    print("This is older than 90 days")

that it : )

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read