If you want to learn how to calculate age in Python, the short answer is this: subtract the birth year from the current year, then subtract one more year if the birthday has not happened yet this year. Three lines of standard-library code do it, and the result is exact. A person born on 15 May 1990 is 36 years old on 16 November 2026, not 36.5, and not 37.
This guide shows you how to calculate age in Python four ways, and every snippet below was actually run on Python 3.12.3 on 16 November 2026, so you get real outputs, not guessed ones: the plain datetime method, the years-months-days breakdown, the pandas method for a whole column of birthdays, and the 29 February edge case most tutorials get wrong. If you only need one function, take Method 1 and stop reading.

Table of Contents
How to Calculate Age in Python with datetime (Method 1)
If you are learning how to calculate age in Python for the first time, start here. The datetime method uses the datetime module, which ships with every Python installation, so you need nothing to install. The trick is a tuple comparison: compare (today’s month, today’s day) with (birth month, birth day). If today’s pair is smaller, the birthday is still to come this year, so you subtract one.
from datetime import date
def calculate_age(birth_date, on=None):
today = on or date.today()
age = today.year - birth_date.year
if (today.month, today.day) < (birth_date.month, birth_date.day):
age -= 1
return age
print(calculate_age(date(1990, 5, 15), date(2026, 11, 16))) # 36
print(calculate_age(date(2000, 2, 29), date(2026, 11, 16))) # 26
print(calculate_age(date(2010, 12, 1), date(2026, 11, 16))) # 15
Output (run on Python 3.12.3, reference date 16 November 2026):
36
26
15
The third example catches beginners out: born 1 December 2010 looks 16 by year subtraction alone (2026 − 2010), but the birthday is still two weeks away, so the real age is 15. Once you know how to calculate age in Python this way, the same comparison protects you from that off-by-one error in every program you write.
Python compares tuples element by element, so (11, 16) < (12, 1) is True because 11 is less than 12 — the same as checking the month first and then the day, in one line.
The optional on date makes the function testable: a 15 May 1990 birthday gives 35 on 14 May 2026 and 36 on 15 May 2026.
print(calculate_age(date(1990, 5, 15), date(2026, 5, 14))) # 35
print(calculate_age(date(1990, 5, 15), date(2026, 5, 15))) # 36
print(calculate_age(date(1990, 5, 15), date(2030, 1, 1))) # 39
Output:
35
36
39
How to Calculate Age in Python in Years, Months and Days (Method 2)
Whole years are enough for most apps, but forms, medical records and HR systems usually want the full breakdown: years, months and days. Writing that by hand means borrowing days across months of different lengths, which is exactly the kind of arithmetic that breeds bugs. The dateutil library does it for you with relativedelta, and this is the method to reach for when you need to know how to calculate age in Python in years, months and days rather than whole years alone.
In this workspace dateutil is already installed (version 2.8.2 with Python 3.12.3). It is not part of the standard library, so if your machine lacks it, run pip install python-dateutil — nothing else in the code below changes.
from datetime import date
from dateutil.relativedelta import relativedelta
today = date(2026, 11, 16)
delta = relativedelta(today, date(1990, 5, 15))
print(delta.years, "years,", delta.months, "months,", delta.days, "days")
delta2 = relativedelta(today, date(2000, 2, 29))
print(delta2.years, "years,", delta2.months, "months,", delta2.days, "days")
Output (run on Python 3.12.3, dateutil 2.8.2):
36 years, 6 months, 1 days
26 years, 8 months, 18 days
So a person born on 15 May 1990 is 36 years, 6 months and 1 day old on 16 November 2026 — six full months after the May birthday, leaving one day. That is the exact relativedelta breakdown the top-ranking tutorials never show: they print the code but never give the numbers for a named birthdate, so a reader cannot check whether it works.
A plain subtraction gives the total days lived as a timedelta:
print((date(2026, 11, 16) - date(1990, 5, 15)).days) # 13334
print((date(2026, 11, 16) - date(2000, 2, 29)).days) # 9757
Output:
13334
9757
Both day counts were cross-checked with the system date command: adding 13,334 days to 15 May 1990 lands on Monday 16 November 2026. If you want the same figures without code, our guide to how to calculate age in days explains the day-count method.
How to Calculate Age in Python for a Whole Spreadsheet (Method 3)
One birthday is a function. A thousand birthdays is a pandas column, and this is where most tutorials stop being useful: they show a single date and leave you to guess how the code scales. The pandas method below was run end to end on a small table, and it includes the trap that breaks the naive version.
First, the trap. When pandas reads a CSV file, date columns arrive as plain text, not dates. Subtracting a string from a Timestamp does not give an age — it raises an error, verified in this workspace:
TypeError: unsupported operand type(s) for -: 'Timestamp' and 'str'
The fix is one line: convert the column with pd.to_datetime before any arithmetic. There is a second trap behind the first. The tuple comparison from Method 1 cannot be used on a whole column, because comparing two pandas Series with < inside an if raises ValueError: The truth value of a Series is ambiguous. Build the birthday check from vectorised comparisons instead.
Here is the full worked example, run on pandas 2.1.4:
import pandas as pd
df = pd.DataFrame({
"name": ["Aisha", "Bilal", "Chen"],
"birthdate": ["1990-05-15", "2000-02-29", "2010-12-01"],
})
today = pd.Timestamp("2026-11-16")
# Step 1: strings become real dates — without this, the subtraction fails.
df["birthdate"] = pd.to_datetime(df["birthdate"])
# Step 2: has this person's birthday happened yet this year?
birthday_passed = (
(df["birthdate"].dt.month < today.month)
| ((df["birthdate"].dt.month == today.month)
& (df["birthdate"].dt.day <= today.day))
)
df["age"] = today.year - df["birthdate"].dt.year - (~birthday_passed).astype(int)
df["age_days"] = (today - df["birthdate"]).dt.days
print(df.to_string(index=False))
Output:
name birthdate age age_days
Aisha 1990-05-15 36 13334
Bilal 2000-02-29 26 9757
Chen 2010-12-01 15 5829
Chen’s age is 15, not 16 — the vectorised check handles the December birthday correctly, exactly like the single-date function. The same code works straight from a CSV file, because pd.read_csv gives you the same string column the example starts with. Anyone learning how to calculate age in Python for data work should treat that conversion as mandatory: it is the single most common cause of the TypeError above.
Reading a Birthdate from User Input
Real programs rarely have the birthdate hard-coded. They read it as text and parse it with strptime, using format codes: %Y for the four-digit year, %m for the month and %d for the day.
from datetime import datetime, date
birth = datetime.strptime("1990-05-15", "%Y-%m-%d").date()
print(birth) # 1990-05-15
print(calculate_age(birth, date(2026, 11, 16))) # 36
Output:
1990-05-15
36
Always wrap parsing in a try/except ValueError block in a real app, because strptime rejects impossible dates such as 2026-02-30 and wrong formats such as 15/05/1990. Also reject future birthdates explicitly — a birthdate after today is bad input, not a negative age. Once you can parse a date string, you already know how to calculate age in Python from real user input; the rest is the same function. The top tutorial mentions invalid dates in passing but never shows the check running, so its readers copy a function that accepts a birthdate in 2030 without complaint.
The 29 February Problem (Leap Year Birthdays)
This is the edge case that separates a correct age calculator from one that is wrong for one person in every 1,461. Someone born on 29 February 2000 has a birthday that only exists in leap years — 2000, 2004, 2008, 2012, 2016, 2020 and 2024 since their birth. So when do they turn a year older in a non-leap year such as 2026: on 28 February or on 1 March? Anyone asking how to calculate age in Python for a leap-day birthday needs this section, because the answer changes with the rule you choose.
It depends on the rule you choose, and the two methods in this article choose differently. I ran both on the same dates:
| Reference date | datetime tuple method | relativedelta |
|---|---|---|
| 28 February 2026 | 25 | 26 years exactly |
| 1 March 2026 | 26 | 26 years, 1 day |
The tuple method compares (2, 28) with (2, 29), finds the birthday has not arrived, and says 25 — so under that method the birthday effectively falls on 1 March. Relativedelta, by contrast, treats 28 February 2026 as the 26th birthday exactly. Neither result is a bug; they are two different conventions. What matters is picking one and saying so.
This site follows the 28 February rule: a 29 February birthday falls on 28 February in non-leap years. That matches relativedelta, so if you want your Python code to agree with this site’s calculator, use Method 2 for leap-day birthdays, or adjust Method 1 by treating 29 February as 28 February when the current year is not a leap year. The competing tutorial claims its method “handles February 29 birthdays” but never tests a single February date — its only leap-year example is a suggestion to “test with date(2000, 2, 29)”, with no output shown.
Three Mistakes That Give the Wrong Age
Mistake 1: dividing days by 365. It looks sensible, but it is wrong: 13,334 days divided by 365 gives 36.53, and divided by 365.25 gives 36.51. Neither is 36, and neither tells you whether to round up or down, because leap years do not spread evenly. Use the tuple comparison or relativedelta instead. If a decimal figure is what you actually need, read our explainer on what decimal age means first.
Mistake 2: using datetime.now() when you mean today’s date. datetime.now() includes the time of day and the local time zone, so the same code can give a different age depending on when and where it runs. For a date of birth, date.today() is the right call — a birthday is a calendar date, not a moment in time.
Mistake 3: trusting code you have not run. The top five results for this keyword currently come from a single domain, copyprogramming.com, and they repeat the same snippet across near-identical pages. The snippet is correct for whole years, but none of those pages shows a verified end-to-end output, none gives the exact relativedelta breakdown for a named birthdate, and none works a pandas CSV example. One of them also claims Python 3.14 is the latest stable version, while the official Python documentation page I opened for this article is headed Python 3.15.2. Every number in this article was run on Python 3.12.3 on 16 November 2026 and can be re-run from the snippets above.
Which Method Should You Use?
| Situation | Best method | Why |
|---|---|---|
| One age in a script or web form | Method 1: datetime tuple | No installs, three lines, exact whole years |
| Years, months and days | Method 2: relativedelta | Handles month lengths and leap years for you |
| A CSV or database column of birthdays | Method 3: pandas | Vectorised, but remember pd.to_datetime first |
| Age on a past or future date | Method 1 with the on parameter | Same logic, any reference date |
| Age in another language | JavaScript or Sheets version | Same formula, different syntax |
If you only need one answer to the question of how to calculate age in Python, take Method 1: it is the standard approach, it needs no third-party library, and it is the method the official Python datetime documentation supports directly. If you work in JavaScript instead, the same birthday logic applies — see our guide to calculating age in JavaScript — and spreadsheet users can follow the same formula in our guide to calculating age in Google Sheets.
Conclusion
You now know how to calculate age in Python four ways: the datetime tuple comparison for a single exact age, relativedelta for years-months-days, pandas for a whole column of birthdays, and the 28 February rule for leap-day birthdays. The numbers here are real — 36 years for a 15 May 1990 birthday, 36 years 6 months 1 day by relativedelta, 13,334 days lived, all as of Monday 16 November 2026 — because every snippet was run before it was written down. Copy Method 1 into your project, run it against a birthday you know, and check it against this site’s calculator.
How do I calculate age in Python from a date of birth?
Subtract the birth year from the current year, then subtract one if the birthday has not happened yet this year: age = today.year - birth.year - ((today.month, today.day) < (birth.month, birth.day)). A person born on 15 May 1990 is 36 on 16 November 2026.
How do I calculate age in years, months and days in Python?
Use relativedelta from dateutil: delta = relativedelta(date.today(), birth_date), then read delta.years, delta.months and delta.days. For a 15 May 1990 birthday on 16 November 2026 the result is 36 years, 6 months and 1 day.
How do I calculate age in Python with pandas?
Convert the birthdate column first with pd.to_datetime(df["birthdate"]), then compute the age with vectorised month and day comparisons, as shown in Method 3. Skipping the conversion raises a TypeError, and the plain tuple comparison cannot be used on a whole column.
How does Python handle a 29 February birthday?
It depends on the method. The datetime tuple method treats the birthday as 1 March in non-leap years, so a person born on 29 February 2000 is still 25 on 28 February 2026 and turns 26 on 1 March. Relativedelta treats 28 February as the birthday and gives exactly 26 years on that day. This site follows the 28 February rule, so use relativedelta if your code must match it.
Do I need to install anything to calculate age in Python?
No, for whole years — the datetime module is part of the standard library. You only need an install for the extras: pip install python-dateutil for relativedelta, and pip install pandas for the spreadsheet method. All three were verified working on Python 3.12.3.
Why is dividing days by 365 the wrong way to calculate age?
Because leap years do not spread evenly, so no single divisor is exact. For a 15 May 1990 birthday, 13,334 days divided by 365 gives 36.53, while the real age on 16 November 2026 is 36.