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

Hello how do i find the sum of two dates in odoo?

Examples: 

start_date= fields.Date(string="Start Date"),

end_date= fields.Date(string="End Date")

total_days=?

Please help i'm new to odoo

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

Hi Daniel Abaka,

I think this will help you.

total_days= fields.Integer(compute="_compute_days", string='Days Count', copy=False, default=0, store=True)

    @api.multi
    @api.depends('end_date', 'start_date')
def _compute_days(self): for s in self: if s.end_date and s.start_date:
a = s.end_date - s.start_date s.total_days = a.days else: s.total_days = 0

regards,

Nikhilkrishnan

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

Thank you

Найкраща відповідь

Hi, 

You can make the total days field as a compute field and compute it's values from the entered values. 


Inside the compute function you can get the difference of days by subtracting the dates, see the sample.

If we have d1 as date1 and d2 as date2 , then difference = (d2-d1).days.

 Also see this link: https://stackoverflow.com/questions/151199/how-to-calculate-number-of-days-between-two-given-dates

Thanks

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

Hi Nikhil krishnan,
Thanks for the support but i'm facing error on this line

s.total_days = a.days
error message there is no attribute for days
Аватар
Відмінити

Actually i test it in my system and working fine, so can you please show the screen shot of the error. and the python code too.

put

print(a)

print(type(a))

before

s.total_days = a.days

show the result to us.

Автор

Here is my python code

depature_date = fields.Date(string="Depature Date From Work", required=True)

return_date = fields.Date(string="Return Date To Work", required=True)

total_number_of_days = fields.Integer(compute="_compute_days",string="Total Number Of Work Days", copy=False, default=0, store=True)

@api.multi

@api.depends('depature_date','return_date')

def _compute_days(self):

for s in self:

if s.depature_date and s.return_date:

a = s.depature_date + s.return_date

s.total_number_of_days = a.days

else:

s.total_number_of_days = 0

here is the error message

s.total_number_of_days = a.days

AttributeError: 'str' object has no attribute 'days'

a = s.depature_date + s.return_date

why you are adding?

what is your logic? can you explain what is your exact need?

Автор

I'm adding depature_date and return_date to get the total_number_of_days .

Автор

Hello NIKHIL KRISHNAN,

The logic is once depature_date and return_date is selected automatically the total_number_of_days should be counted as total days selected.

I'm this ways i want to add depature_date + return_date to get the total days.

ok fine,

You try with this code.

a = s.return_date - s.depature_date

s.total_number_of_days = a.days

if we add two dates, we can't get a total number of days, differece of dates means the number of days between the two dates. i think you got the idea.

upvotes are appreciated

Автор

NIKHIL KRISHNAN ,

I have try this code and get error

a = s.return_date - s.depature_date

s.total_number_of_days = a.days

Error Message

in _compute_days

a = s.depature_date - s.return_date

TypeError: unsupported operand type(s) for -: 'str' and 'str'

please convert them in to date formate.

Related Posts Відповіді Переглядів Дія
1
трав. 20
6591
1
січ. 19
6014
3
груд. 18
8886
0
лист. 18
5511
0
трав. 22
3248