Compare dates in an array?

Asked

Viewed 518 times

1

In the date variable, you are receiving several dates in string as array, want to bring the smallest

cont = 0
menor_data = datetime.strptime('31/12/2300', '%d/%m/%Y')
while cont < len(datas):
    data_d = datetime.strptime(datas[cont], '%d/%m/%Y').date()
    if data_d < menor_data:
        menor_data = data_d[cont]
    cont = cont + 1
print(menor_data)

Returns the following error:

Traceback (Most recent call last):

in

if data_d < menor_data:

Typeerror: can’t compare datetime.datetime to datetime.date

  • 1

    In this case, the error message tells you exactly what is happening. I do not know if it is productive a complete answer - the connection is that you try to read the error message. If you don’t even know English, use an automatic translator (but it can get worse, since the translator won’t know what a class name is and what a phrase is)

  • 2

    in your variable menor_data tries to put the method . date() to return the same type you are creating inside while. They must be of the same type, or datetime or date.

  • 1

    I understand the message, but was not seeing that the missing . date(), Thanks!

  • @Marlysson I suggest you write your comment as an answer so that the OP can accept your answer, and so mark the question as answered.

  • @dot. Py Blz. As suggested I put my comment as response, if it helped solve the problem mark it as correct for further queries.

1 answer

2


In your variable menor_data tries to put the method .date() to return the same type you are creating within the while.

They must be of the same type, or datetime or date.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.