How to Calculate Age from Birthday in Python

Age is an important detail for many reasons. Perhaps the most important is that it is required for certain legal purposes. For example, in order to vote or to buy alcohol, you must be of a certain age. Other reasons to keep track of age may include eligibility for discounts or other benefits.

In order to calculate age in an app, you need to know two pieces of information: the person’s date of birth and the current date. You can then use a simple math formula to calculate the difference between the two dates. This difference is the person’s age in years.

from datetime import date

today = date.today()
age = today.year - born.year - ((today.month, today.day) < (born.month, born.day))
def cfp_rate_singapore(self, employee):
        rate = {'employee': 20, 'employer': 17}
        born = employee.birthday
        if born:
            today = date.today()
            age = today.year - born.year - ((today.month, today.day) < (born.month, born.day))

            cfp = self.env['sigma2k.salary.cfp'].search([('age_from', '<', age), ('age_to', '>=', age)], limit=1)
            rate = {'employee': cfp.employee_rate, 'employer': cfp.employer_rate}

        return rate