Skip to Content
Menu
This question has been flagged
1 Reply
1162 Views

Hello

I'm trying to write a function that can compare the current date with one stored in the table to see if the current date is greater than the stored one .

i have tried every possible way to do that but i ended up with nothing !!!

the code just do nothing at the if statement . no error , no nothing at all.


Here is my code:

date_eval = datetime.now()

date_eval_str = date_eval.strftime('%Y-%m-%d')

warranty_records = self.env['warranty.details'].search([])


for val in warranty_records:

    new_date = str(val.warranty_end_date)

    print(new_date)

    print(date_eval_str)

    if new_date <= date_eval_str:

        print("test")

        print(val.state)

        val.state = 'expired'

.and here what i  get from printing both of them before the it statement
2021-05-21
2021-05-08
2023-10-27
2021-05-08

i hope anyone can have any clue on what may cause this problem.

Thanks 
 


Avatar
Discard
Best Answer

Hi,

For comparing dates , you need not convert them into string  using strftime  then compare, you can directly compare

current_date = fields.Date.context_today(self)
if current_date > warranty_end_date :
    your conditions

Thanks

 

 

Avatar
Discard