Odoo Help


Calculate total days from two dates

on 12/16/15, 2:21 AM 842 views

I have three fields 'start_date', 'end_date' and total_days.

If i enter 'start_date' and 'end_date' field it should automatically display total_days. 

This is my Function which is not working. 

Is there any other method? or what may be the issue

<field name="start_date" on_change="get_number_of_days(start_date,end_date)"/>
<field name="end_date" on_change="get_number_of_days(end_date,start_date)"/>
<field name="total_days" />
def get_number_of_days(self, cr, uid, ids, start_date, end_date):
"""Returns a float equals to the timedelta between two dates given as string."""
result = self.browse(cr, uid, ids)
if (end_date and start_date) and (start_date <= end_date):
DATETIME_FORMAT = "%d/%m/%Y %H:%M:%S"
from_dt = datetime.datetime.strptime(start_date, DATETIME_FORMAT)
to_dt = datetime.datetime.strptime(end_date, DATETIME_FORMAT)
timedelta = to_dt - from_dt
diff_day = timedelta.days + float(timedelta.seconds) / 86400
result['value']['total_days'] = round(math.floor(diff_day))+1
return result 

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 12/16/15, 2:21 AM
Seen: 842 times
Last updated: 12/16/15, 4:33 AM