Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
1564 Переглядів

Hello Team,

I am using Odoo 14 Studio to calculate number of days between two dates and this is what I used and it is not working


for record in self:

If record.x_studio_start_date > record.x_studio_end_date:

raise ValidationError('Start date should not greater than end date.')

else:

record.x_studio_expected_no_of_days = (record.x_studio_end_date - record.x_studio_start_date).days

Thanks for all your help


Аватар
Відмінити
Найкраща відповідь

Hi, so if I don't have the error message, then the whole code to put in Studio would be : 

for record in self:
        start_date = datetime.strptime(record.x_studio_start_date, '%Y-%m-%d')
        end_date = datetime.strptime(record.x_studio_end_date, '%Y-%m-%d')
        delta = end_date - start_date
        record.x_studio_expected_no_of_days = delta.days

Why do I have an error message then ?
Is there a change if the format of the date is %d-%m-%Y

Thanks in advance for the help

Аватар
Відмінити
Найкраща відповідь

Hi,

You can try this code while importing the datetimefor record in self:
    if record.x_studio_start_date > record.x_studio_end_date:
        raise ValidationError('Start date should not be greater than the end date.')
    else:
        start_date = datetime.strptime(record.x_studio_start_date, '%Y-%m-%d')
        end_date = datetime.strptime(record.x_studio_end_date, '%Y-%m-%d')
        delta = end_date - start_date
        record.x_studio_expected_no_of_days = delta.days

Hope it helps

Аватар
Відмінити