An age calculator is one of the best first projects in any language: small enough to finish in an afternoon, tricky enough to teach you real lessons about dates. This tutorial builds one three ways โ JavaScript (for the browser), Python (for scripts), and Excel (no code at all) โ and covers the traps that make most first attempts subtly wrong. It's the same logic that powers our own age calculator.
What is the formula for an age calculator?
Every accurate age calculator implements one algorithm โ compare the calendar parts of two dates, borrowing when a subtraction goes negative:
months = today.month โ birth.month (if < 0: months += 12, years โ= 1)
days = today.day โ birth.day (if < 0: borrow previous month's length, months โ= 1)
The tempting alternative โ count total days and divide by 365.25 โ drifts because leap years aren't evenly spread. Calendar comparison is exact by construction. (Full explanation in How Age is Calculated.)
Age calculator in JavaScript
The complete logic in one function โ paste it into any page:
function calculateAge(birthDateString) {
const birth = new Date(birthDateString);
const today = new Date();
let years = today.getFullYear() - birth.getFullYear();
let months = today.getMonth() - birth.getMonth();
let days = today.getDate() - birth.getDate();
if (days < 0) {
months--;
// days in the month BEFORE the current one
days += new Date(today.getFullYear(), today.getMonth(), 0).getDate();
}
if (months < 0) { years--; months += 12; }
return { years, months, days };
}
console.log(calculateAge("1996-09-25"));
// โ { years: 29, months: 9, days: 17 } (on 12 July 2026)
Hook it to an input with a few lines of HTML:
<input type="date" id="dob">
<button onclick="show()">Calculate</button>
<p id="out"></p>
<script>
function show() {
const a = calculateAge(document.getElementById("dob").value);
document.getElementById("out").textContent =
`${a.years} years, ${a.months} months, ${a.days} days`;
}
</script>
The key line is new Date(year, month, 0).getDate() โ day 0 of a month is the last day of the previous month, which hands you the correct borrow amount (28, 29, 30, or 31) with leap years handled by the Date object itself.
Age calculator in Python
Same algorithm with the standard library only:
from datetime import date
import calendar
def calculate_age(birth: date, today: date | None = None):
today = today or date.today()
years = today.year - birth.year
months = today.month - birth.month
days = today.day - birth.day
if days < 0:
months -= 1
prev_month = today.month - 1 or 12
prev_year = today.year if today.month > 1 else today.year - 1
days += calendar.monthrange(prev_year, prev_month)[1]
if months < 0:
years -= 1
months += 12
return years, months, days
y, m, d = calculate_age(date(1996, 9, 25))
print(f"{y} years, {m} months, {d} days")
calendar.monthrange(year, month)[1] returns the number of days in a month, leap years included. If you're allowed a third-party package, dateutil reduces the whole thing to two lines:
from dateutil.relativedelta import relativedelta
age = relativedelta(date.today(), date(1996, 9, 25))
print(age.years, age.months, age.days)
Age calculator in Excel โ no code
One formula, with the date of birth in A1:
=DATEDIF(A1,TODAY(),"Y") & " years, " &
DATEDIF(A1,TODAY(),"YM") & " months, " &
DATEDIF(A1,TODAY(),"MD") & " days"
That's the entire calculator. For age in days only, =TODAY()-A1 does it. Our full Excel age calculation guide covers every variant, plus DATEDIF's known end-of-month quirk.
The bugs that break first attempts
- Subtracting years only.
2026 โ 1996 = 30overstates age for everyone whose birthday hasn't passed. The borrow logic exists precisely for this. - Dividing days by 365. Off by roughly a day per four years lived โ see the Leap Year Guide for why.
- Time zones in JavaScript.
new Date("1996-09-25")parses as UTC midnight; in negative-offset time zones it can render as 24 September local time, shifting results by a day. Parsing the parts explicitly โnew Date(1996, 8, 25)โ keeps everything in local time. - Borrowing from the wrong month. The borrow always comes from the month before the current date, not the birth month.
- Not validating input. A future birth date should produce an error, not a negative age.
Bonus: the pocket calculator age trick
Want to work out someone's age with a plain calculator and a bit of theatre? Have them do this secretly:
- Take the day of the month they were born (e.g. 25)
- Multiply by 20, add 3, multiply by 5, then add their birth month number (Sept โ 9): now the display encodes day and month
- Multiply by 20, add 3, multiply by 5, then add their age
- Have them announce the total โ you subtract 1515
Reading the result right to left: the last two digits are the age, the next two the month, the rest the day. For day 25, month 9, age 29 the display reads 25 09 29. The multiply-by-100 structure (20 ร 5) just shifts each answer two digits left, and the +3 ร5 steps add the 1515 you remove at the end โ arithmetic dressed up as a mind trick.
Frequently asked questions
Or skip the build entirely
The finished product โ exact age in every unit, free, and private.
Use the Age Calculator โ