Python
Age Calculation
DateTime
Programming
Birthdate

Age from birthdate in python

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To calculate age from a birthdate in Python, the usual approach is to subtract the birth year from the current year and then adjust if the birthday has not happened yet this year. That sounds simple, but doing it correctly means comparing month and day rather than dividing a raw day count by 365.

The standard datetime.date solution

python
1from datetime import date
2
3def age_from_birthdate(birthdate, today=None):
4    if today is None:
5        today = date.today()
6
7    age = today.year - birthdate.year
8
9    if (today.month, today.day) < (birthdate.month, birthdate.day):
10        age -= 1
11
12    return age
13
14print(age_from_birthdate(date(1990, 10, 20)))

This works because age is really "how many birthdays have passed", not "how many days divided by a rough year length".

Why subtracting days is not enough

A tempting but incorrect approach is:

python
(today - birthdate).days // 365

That fails around leap years and birthdays because calendar years are not all the same length. Age is a calendar concept, not a fixed number of days.

A concrete example

python
1from datetime import date
2
3birthdate = date(2000, 12, 31)
4today = date(2025, 1, 1)
5
6print(age_from_birthdate(birthdate, today))  # 24

Even though the year numbers differ by 25, the birthday has not happened yet in 2025, so the correct age is 24.

Handling leap-day birthdays

Leap-day birthdays require a policy decision. If someone was born on February 29, how should age be handled in non-leap years? Many systems simply keep the same month and day comparison logic and rely on the calendar meaning of the date.

If your domain needs a special rule, such as treating February 28 or March 1 as the birthday in non-leap years, implement that rule explicitly rather than assuming there is one universally correct answer.

Parsing from strings

If the birthdate arrives as a string, parse it first:

python
1from datetime import datetime
2
3birthdate = datetime.strptime("1990-10-20", "%Y-%m-%d").date()
4print(age_from_birthdate(birthdate))

Separating parsing from age calculation keeps the logic easier to test.

Using dateutil.relativedelta

If you already use python-dateutil, relativedelta can express calendar differences directly.

python
1from datetime import date
2from dateutil.relativedelta import relativedelta
3
4birthdate = date(1990, 10, 20)
5today = date.today()
6
7age = relativedelta(today, birthdate).years
8print(age)

This is convenient, but it adds a dependency. For many scripts, the standard-library function is perfectly adequate.

Use date, not full timestamps, when possible

Age is usually a calendar concept, not a time-of-day concept. Converting to date objects early avoids confusing edge cases where time zones or hours and minutes make the subtraction logic harder than it needs to be.

Validate impossible inputs

A future birthdate should usually be rejected:

python
1def age_from_birthdate(birthdate, today=None):
2    if today is None:
3        today = date.today()
4
5    if birthdate > today:
6        raise ValueError("birthdate cannot be in the future")
7
8    age = today.year - birthdate.year
9    if (today.month, today.day) < (birthdate.month, birthdate.day):
10        age -= 1
11    return age

This makes the function safer in forms, APIs, and data-cleaning pipelines.

Common Pitfalls

  • Dividing day differences by 365 and calling that age.
  • Forgetting to adjust when the birthday has not occurred yet this year.
  • Ignoring future birthdates and returning negative ages.
  • Mixing datetime and date objects without normalizing them.
  • Assuming leap-day birthdays need no special business-rule discussion.

Summary

  • The correct age calculation compares year, month, and day, not just day counts.
  • Subtract the years, then reduce by one if the birthday has not happened yet.
  • Parse string input into a date before calculating age.
  • Reject future birthdates explicitly when appropriate.
  • Use relativedelta if you want a calendar-aware helper from python-dateutil.

Course illustration
Course illustration

All Rights Reserved.