Odoo Help


how to calculate difference bitween two dates ?

on 9/18/13, 12:25 PM 10,595 views

Hi, in my object I have two date fields: date1:fields.date('date 1'), date2:fields.date('date 2'),

I want to retrieve the difference of that 2 fields I tried this : date2 - date1 but I got this error : TypeError: unsupported operand type(s) for -: 'str' and 'str'

to do this, the first you must convert into date format (use lib datetime: datetime.date(year, month, day)). Then you can use operator '-' in this case. Refer: http://docs.python.org/2/library/datetime.html

OpenERP Vietnam
on 9/19/13, 3:25 AM

thank you !

on 9/19/13, 6:03 AM
On 9/19/13, 3:25 AM

Hi mos, You need to do some python level stuffs with dates for getting exact date difference , i will explain with an example

from datetime import datetime

fmt = '%Y-%m-%d'
from_date = '2013-09-01'     -> Take your date field1
to_date = '2013-09-31'       -> Take your date field2

d1 = datetime.strptime(from_date, fmt)

d2 = datetime.strptime(to_date, fmt)

daysDiff = str((d2-d1).days)          -> will gives you 29 days

daysDiff = str((d2-d1).days + 1)      -> will gives you 30 days

Hope it helps you ........ Thanks

thank you !

on 9/19/13, 6:02 AM

hi nishad thanks for your answer i want to calculate it automatically and show in readonly=True field so can you please provide me some extra detail for calculate duration

Atul Kumar jain
on 3/18/14, 5:44 AM

Define two date fields(from date , to date) & one functional field(difference_days) to keep the difference days. Do implement the date difference calculation steps in your function and return the value . A functional field in openerp will return a readonly = True value by default. If you want to save data in to DB then use store=True parameter while defining functional field.

on 3/18/14, 7:17 AM

my field is 'date_s':fields.datetime('Start Date'), 'date_e':fields.datetime('End Date'), 'duration':fields.function(_get_days, string='Duration', store=True), and my function is this def _get_days(self, cr, uid, ids, field_name, args, context=None): res = {} for date in self.browse(cr, uid, ids, context=context): res[date.id] = ((date.date_s-date.date_e).days) or False return res but it give me error like this res[date.id] = ((date.date_s-date.date_e).days) or False TypeError: unsupport

Atul Kumar jain
on 3/18/14, 7:33 AM

You need to include 'from datetime import datetime' in the import section and then do a date formatting using 'datetime.strptime(from_date, fmt)' for each date fields where fmt = '%Y-%m-%d' , because we are doing a conversion from openerp datetime format to native python format and then calculating the difference . Do one by one as i shown in my source code instead of doing it in single step.

on 3/18/14, 7:48 AM

i correct my function but still i got error like this data_string[found.end():]) ValueError: unconverted data remains: 10:52:57 and my function is def _get_days(self, cr, uid, ids, field_name, args, context=None): res = {} for date in self.browse(cr, uid, ids, context=context): fmt = '%Y-%m-%d' date.date_s = datetime.strptime(date.date_s, fmt) date.date_e = datetime.strptime(date.date_e, fmt) res[date.id] = ((date.date_s-date.date_e).days) or False return res

Atul Kumar jain
on 3/18/14, 7:55 AM

This is because your fields are of type' datetime' , instead use normal 'date' in openerp.

on 3/18/14, 8:16 AM

i already do that but still got erro

Atul Kumar jain
on 3/18/14, 8:17 AM

hi nishad please help me

Atul Kumar jain
on 3/18/14, 8:40 AM

def _get_days(self,cr,uid,ids,field_name, arg ,context=None):
res = {}, fmt = '%Y-%m-%d', for object in self.browse(cr, uid, ids, context=context): res[object.id] = {'diff_days':0, } from_date = object.date_a, to_date = object.date_b, d1 = datetime.strptime(from_date, fmt), d2 = datetime.strptime(to_date, fmt) ,daysDiff = str((d2-d1).days), res[object.id]['diff_days'] = daysDiff , return res

on 3/18/14, 9:40 AM

'date_a':fields.date("Date1"), 'date_b':fields.date("Date2"), 'diff_days':fields.function(_get_days,string="Diff days", multi='sums',store=True),

on 3/18/14, 9:45 AM

Hi nishad thanks for your reply i solve this problem

Atul Kumar jain
on 3/19/14, 3:19 AM
On 11/15/16, 7:27 AM

Use timedelta object form python

from datetime import timedelta


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: 9/18/13, 12:25 PM
Seen: 10595 times
Last updated: 3/7/17, 4:10 AM