Age Calculator: How to Calculate Your Exact Age in Years, Months, and Days
Calculating someone's exact age sounds trivial — subtract the birth year from the current year. But the moment you need months and days, or account for leap years and timezone differences, the problem gets more interesting. This guide explains every detail, from the simple formula to production-ready code in four languages.
The Simple Formula (Years Only)
Age (years) = Current Year − Birth Year − (1 if birthday hasn't occurred yet this year, else 0)
Example: born 15 March 1990, today is 10 July 2026.
2026 − 1990 = 36 years
Birthday (March 15) has already passed in 2026 → no subtraction
Age = 36 years
If today were 1 February 2026 (before March 15), the birthday hasn't happened yet, so age = 35.
Full Age: Years, Months, Days
For a precise breakdown you need to compare month and day components, not just years.
Step-by-step algorithm
- Start with
years = currentYear − birthYear. - Compare
(currentMonth, currentDay)with(birthMonth, birthDay).- If the current month/day is before the birthday → subtract 1 year, and set the "effective" start of the remaining period to the birthday in the previous year.
- Count remaining complete months between the adjusted birthday and today.
- If the current day is before the birth day of the month → subtract 1 month.
- Count remaining days (current day minus effective birth day, adjusted for different month lengths).
This logic is identical to what banks use for calculating interest periods and what HR systems use for probation checks.
Calculating Age in Other Units
| Unit | Formula |
|---|---|
| Total days | (today − birthDate).totalDays |
| Total hours | totalDays × 24 |
| Total minutes | totalDays × 1440 |
| Total seconds | totalDays × 86400 |
| Total weeks | floor(totalDays / 7) |
| Total months (approx.) | years × 12 + completedMonths |
"Approximate" matters for months because calendar months have different lengths. Libraries like date-fns (JS) or relativedelta (Python) handle this correctly.
Code Examples
JavaScript
function calculateAge(birthDateStr) {
const birth = new Date(birthDateStr);
const now = new Date();
let years = now.getFullYear() - birth.getFullYear();
let months = now.getMonth() - birth.getMonth();
let days = now.getDate() - birth.getDate();
if (days < 0) {
months -= 1;
// Days in the previous month
const prevMonth = new Date(now.getFullYear(), now.getMonth(), 0);
days += prevMonth.getDate();
}
if (months < 0) {
years -= 1;
months += 12;
}
const totalDays = Math.floor((now - birth) / (1000 * 60 * 60 * 24));
return { years, months, days, totalDays };
}
const age = calculateAge("1990-03-15");
console.log(`${age.years}y ${age.months}m ${age.days}d`); // e.g. 36y 3m 25d
console.log(`${age.totalDays} days alive`);
Important: new Date("1990-03-15") is parsed as UTC midnight. If the user's local timezone is behind UTC, new Date() and the parsed date may land on different calendar days. Always test around midnight boundaries in production.
Python
from datetime import date
def calculate_age(birth: date) -> dict:
today = date.today()
years = today.year - birth.year
months = today.month - birth.month
days = today.day - birth.day
if days < 0:
months -= 1
# Days in the previous month
import calendar
prev_month_days = calendar.monthrange(today.year, today.month - 1 or 12)[1]
days += prev_month_days
if months < 0:
years -= 1
months += 12
total_days = (today - birth).days
return {"years": years, "months": months, "days": days, "total_days": total_days}
age = calculate_age(date(1990, 3, 15))
print(f"{age['years']}y {age['months']}m {age['days']}d")
print(f"{age['total_days']} days alive")
For cleaner month arithmetic in Python, use dateutil.relativedelta:
from datetime import date
from dateutil.relativedelta import relativedelta
birth = date(1990, 3, 15)
delta = relativedelta(date.today(), birth)
print(f"{delta.years}y {delta.months}m {delta.days}d")
Go
package main
import (
"fmt"
"time"
)
type Age struct {
Years, Months, Days, TotalDays int
}
func calculateAge(birth time.Time) Age {
now := time.Now()
years := now.Year() - birth.Year()
months := int(now.Month()) - int(birth.Month())
days := now.Day() - birth.Day()
if days < 0 {
months--
// Last day of the previous month
prevMonth := time.Date(now.Year(), now.Month(), 0, 0, 0, 0, 0, now.Location())
days += prevMonth.Day()
}
if months < 0 {
years--
months += 12
}
totalDays := int(now.Sub(birth).Hours() / 24)
return Age{years, months, days, totalDays}
}
func main() {
birth, _ := time.Parse("2006-01-02", "1990-03-15")
age := calculateAge(birth)
fmt.Printf("%dy %dm %dd — %d days alive\n",
age.Years, age.Months, age.Days, age.TotalDays)
}
PHP
function calculateAge(string $birthDateStr): array {
$birth = new DateTimeImmutable($birthDateStr);
$now = new DateTimeImmutable();
$diff = $now->diff($birth);
return [
'years' => $diff->y,
'months' => $diff->m,
'days' => $diff->d,
'total_days' => $diff->days,
];
}
$age = calculateAge('1990-03-15');
echo "{$age['years']}y {$age['months']}m {$age['days']}d\n";
echo "{$age['total_days']} days alive\n";
PHP's DateTime::diff() handles all the edge cases (leap years, month-length differences) internally, making it the cleanest implementation of the four.
Edge Cases to Watch Out For
Leap year birthdays
People born on 29 February (leap day) have a birthday that exists only every four years. Different systems handle this differently:
- Most use 28 February in non-leap years.
- Some use 1 March instead.
- The PHP and Python
dateutilapproaches shown above both default to 28 February.
Make an explicit choice and document it.
Timezone issues
new Date() in JavaScript returns local time; date.today() in Python returns the server's local date. If a user in Tokyo opens a page at 23:30 local time, the server in New York might still be on the previous calendar day. For accurate client-side age display, always calculate in the user's local timezone — or send the calculation to the client.
Historical dates and calendar reforms
The Gregorian calendar was not adopted uniformly. Before 1582 (or much later in some countries), dates are in the Julian calendar, which has a different leap year rule. For everyday birthday calculations this doesn't matter, but genealogy and historical software need to handle it.
Future birth dates
Validate that the birth date is not in the future. Returning a negative age is confusing; it is better to show an error.
Age Milestones Reference
| Milestone | Years |
|---|---|
| Legal adult (most countries) | 18 |
| Car hire without surcharge | 25 |
| Quarter century | 25 |
| Half century | 50 |
| Retirement (EU average) | 65 |
| Centenary | 100 |
Some APIs (e.g. for alcohol purchases, financial products, or medical forms) require checking whether today's date is on or after a milestone. The code structure is the same as above — just compare the full (year, month, day) tuple.
FAQ
How do I calculate age without knowing the exact birth date?
If you only know the birth year, subtract it from the current year for an approximate age (±1 year). If you know the birth month but not the day, you can give a range: "between X and X+1 years old".
Is there a difference between age in completed years and age in running years?
Yes. "Completed years" (also called "last birthday" age) is what most people mean: how many full 365-day (or 366-day) cycles have elapsed since birth. "Running years" counts the current year as 1 even if it hasn't completed yet. Legal and medical contexts always use completed years.
How many days old am I if I'm 30?
Roughly 10,950–10,960 days, depending on how many leap years fell in that period. Use the totalDays value from the code examples above for an exact count.
Why does my age calculator give a different result than another one?
The most common cause is timezone handling. A calculator that runs on a server in a different timezone than the user may report a day off around midnight. Always prefer client-side calculation or pass the user's timezone offset to the server.
Can I calculate a pet's age in human years?
The popular "multiply by 7" rule for dogs is a myth. A more accurate (and still approximate) model for dogs is: the first year equals 15 human years, the second year adds 9, and each subsequent year adds about 4–5. For cats, the first two years equal roughly 24 human years, then about 4 per year thereafter.
Calculate Your Age Now
Skip the maths — use the Age Calculator to find your exact age in years, months, days, hours, minutes, and seconds instantly.