Se rendre au contenu
Menu
Cette question a été signalée
3 Réponses
7231 Vues

Dear all,
I am having problems on comparing 2 dates in Odoo 12.
I have a variable that contains a date. If I print it I get:

>> print(date)
2019-05-11

I also have a field of date type with a calender widget set on xml.

So, when I print it's value I get:

>> print vals.get('date')
2019-05-11

The problem is that I am unable to compare both fields. I get no error and the "if statement" is not read.

if date == vals.get('date'):
    print("Date is equal")

The full code is:

registos = self.env['mymodule.mymodel'].search([('myfield.id','=',value_to_search)])
for rec in registos:
    date = rec.date
    if date == vals.get('date'):
        print("Date is equal")

What's wrong here?

Thank you in advance

Best regards

Paulo



Avatar
Ignorer

Can you just check whether both the fields/value or of same data type...

Auteur

Thank you @Niyas.

This was exactly the problem. Both values had different type values.

vals.get('date') #string

rec.date #date

I have solved the issue.

Thank you once again

Meilleure réponse

Here you are comparing two strings where odoo retrieves date or DateTime fields as a string. So here you are comparing str == str and results vary according to that. The solution is that you have to typecast the fields before comparison using pythons default or odoo's way. I can give you an example.

date = vals.get('date') # String
date = feilds.Date.from_string(date) # Date(01,02,2019) dateobject

Output:-
Avatar
Ignorer
Auteur

Thank you @Hilar,

I solved the problem by converting "rec.date" to string in order to be able to compare both values "as strings", since "vals.get('date')" is already a string. It solved my problem.

Thank you once again

No, Thats wrong. you have to convert it to the date object for comparison. else the result will be wrong.

Auteur

I have already tried @Hilar and I get correct results.

Based on your last comment:

vals.get('date') #string

date.today() #date object

data = vals.get('date') #string

data_hoje = str(date.today()) #string

Using, "if data > data_hoje:" (as an example), gives me exactly the results I expect...

Please check my updated answer and look the last example.

You should understand the logic that the string > string doesn't gonna give you the exact result for comparing two dates, that is Date(10,01,2018) > Date(11,01,2019). See this example
image.png



On Wed, May 15, 2019 at 9:57 PM Paulo Matos <batalhadematos@gmail.com> wrote:

I have already tried @Hilar and I get correct results.

Based on your last comment:

vals.get('date') #string

date.today() #date object

data = vals.get('date') #string

data_hoje = str(date.today()) #string

Using, "if data > data_hoje:" (as an example), gives me exactly the results I expect...

Sent by Odoo S.A. using Odoo.



--
Hilar AK  Python / Odoo Developer

m: +917736617619
e: hilarak@gmail.com
Follow me:
twitteryoutubegithubstack-overflow


Auteur

Thank you @Hilar AK, now I fully understood the logic. You are right. I will try to typecast the fields so I get the desired "safe" results. Thank you once again. Best regards

Meilleure réponse

Avatar
Ignorer
Publications associées Réponses Vues Activité
3
juin 25
1182
1
avr. 25
3644
1
janv. 25
18027
0
mars 24
1383
0
oct. 22
2563