calculate age from DOB – odoo

.py file

'age': fields.char('Age',size=64),

.xml file

<field name="dob" on_change="get_age(dob)"/>

python function

odoo 8
from datetime import date,datetime
def get_age(self, cursor, user, ids, dob, context=None):
    if dob:
        dob = datetime.strptime(dob, "%Y-%m-%d").date()
        today =
        age_cal = relativedelta(today,dob)
        age= str(age_cal.years) + ' Years'
        res =  {'age': age}
    return {'value': res}
odoo 10 import time 
from datetime import datetime
age = fields.Char(string="Age", required=False, )

def get_age(self):
 res = {}
 if self.dob:
 dob = datetime.strptime(self.dob, "%Y-%m-%d")
 age_calc = ( - dob).days/365
 age= str(age_calc) + ' Years'
 self.age = age








Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s