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

I have a 'day/month' string, I want to convert that string to date object and compare the last day of than month to another date
Example:
For '08/2021' (august, 2021) i whant to compare the last day of that month (31-08-2021) to another date (date field),
For '02/2020' i what to compare 29-02-2020 < another_date (date field)
For '02/2021' i what to compare 28-02-2020 < another_date (date field)

Avatar
Discard
Best Answer

Hello, 

You could do something like this:

# get the string field in the format "mm/yyyy" and add 1 to the date
date_object = datetime.datetime.strptime("1/"+record.string_date, "%d/%m/%Y")

# add a month to the date object and minus a day to get last day of month
date_object = date_object + dateutil.relativedelta.relativedelta(months=1) + dateutil.relativedelta. relativedelta(days=-1)

id date_object < record.other_date:
    # if the date field is less than another field, do something

The idea is to get the first day of the field month, add a month to get the first day of the next month and minus a day to get the last day of the field month.

I think this would be the best way to solve this problem using the libraries supplied by Odoo using the UI. This is an example of how it would look in a server action, you just need to change the fields to the fields which are in your database table. 

I hope this helps,

Thanks, 

Avatar
Discard