Odoo Help

Welcome!

This community 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.

1

how to calculate age from date of birth

By
Mohammed Tariq
on 7/1/13, 5:23 AM 7,814 views

How to calculate age from date of birth using functional fileds . I tried all sorts of ways but not getting it properly. I don't know how to write the xml view for it since there is no proper code available for it. Please help since i am new here.

4
Indrabhan Bhamare
On 7/21/15, 1:57 AM

calculating age base on date of birth using @api onchange()

from openerp import fields, models, api, _
from dateutil.relativedelta import relativedelta
from datetime import date

class students(models.Model):
    _name = 'school.student'

    dob = fields.Date('DOB')
    age = fields.Char('Age')


    @api.onchange('dob')
    def set_age(self):
        for rec in self:
    if rec.dob:
    dt = rec.dob
    d1 = datetime.strptime(dt, "%Y-%m-%d").date()
           d2 = date.today()
rd = relativedelta(d2, d1)
rec.age = str(rd.years) + ' years'
4
dianne jose
On 7/2/13, 9:18 AM

python code for age calculation from date of birth:

from datetime import datetime from dateutil import parser

class mark_marksheet(osv.osv):
    _name="mark.marksheet"
    _description="Report Card"
    _columns={
         "dob":fields.date('Date of Birth',required=True),
        'age_text':fields.text('textage'),
    }

def onchange_getage_id(self,cr,uid,ids,dob,context=None):
    current_date=datetime.now()
    current_year=current_date.year
    birth_date = parser.parse(dob)
    current_age=current_year-birth_date.year
     val = {
        'age_text':current_age
    }
    return {'value': val}

mark_marksheet()

Xml View Code

<record id="view_mrk_form" model="ir.ui.view">
    <field name="name">mark.marksheet.form</field>
        <field name="model">mark.marksheet</field>
        <field name="arch" type="xml">
        <form string="Score Card">
            <field name="dob" on_change="onchange_getage_id(dob,context)"/>
            <field name="age_text"/>
        </form>
    </field>
</record>
2
PARVATHY VIJAYAN P
On 7/3/13, 4:38 AM

def onchange_getage_id(self,cr,uid,ids,dob,context=None):

    current_date=datetime.now()
    current_year=current_date.year
    birth_date = parser.parse(dob)
    current_age=current_year-birth_date.year
    val = {
        'age_text':current_age
    }
    return {'value': val}
0
Andrias Yohanson
On 9/24/15, 11:34 PM

Hi Folks,

I try the above code to calculate age, and making some modifcation. It inherit hr.employee modul in hr folder here are my modification:

in python add the import

from openerp.osv import osv,fields
from datetime import datetime
import dateutil.parser
class hr_employee(osv.osv):
    _inherit = 'hr.employee'

    def hitung(self, dob):
        current_date=datetime.now()
        current_year=current_date.year
        birth_date = dateutil.parser.parse(dob)
        current_age=current_year-birth_date.year
        return current_age

    def _calc_age(self, cr, uid,ids, field, arg, context=None):
        results = {}
        employee = self.browse(cr, uid, ids, context=context)
        if employee.dob:
            results[employee.id] = self.hitung(employee.dob)
        return results

    _columns = {
       'dob'         : fields.date('Date Of Birth'),
        'age_text' : fields.function(_calc_age, type='char', string="Age"),
    }
    _defaults = {,
        'dob': fields.date.context_today,
    }

    def onchange_getage_id(self,cr,uid,ids,dob,context=None):
        current_date=datetime.now()
        current_year=current_date.year
        birth_date = dateutil.parser.parse(dob)
        current_age=current_year-birth_date.year
        val = {
            'age_text':current_age
        }
        return {'value': val}  

in xml add these:
<field name="dob" on_change="onchange_getage_id(dob,context)"/>
<field name="age_text"/>

0
Mohammed Tariq
On 7/3/13, 2:15 AM

i am getting an error while defining in this line : val = { 'age_text':current_age } Is this correct ??

Can you post the error message

PARVATHY VIJAYAN P
on 7/3/13, 3:52 AM

I am getting the error in my python file in eclipse

Mohammed Tariq
on 7/3/13, 3:38 PM

Can you post the screen shot of ur python file

PARVATHY VIJAYAN P
on 7/4/13, 8:21 AM

Is that indent error in eclipse

PARVATHY VIJAYAN P
on 7/5/13, 2:48 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 7/1/13, 5:23 AM
Seen: 7814 times
Last updated: 9/25/15, 2:54 AM