Introduction
In this guide you will build an interactive Age Calculator web app using Python and Streamlit. The app calculates your exact age in years, months, and days — and even shows fun insights like the total number of days you have been alive. If you want a different twist on date math, the unit converter app follows the same Streamlit patterns.
Features
- Date Picker — Users select their birthdate with a simple calendar widget.
- Age Breakdown — Displays years, months, and days in a clean metric layout.
- Special Messages — Dynamic messages based on age range (minor, adult, senior).
- Fun Fact — Shows the total number of days the user has been alive.
Prerequisites
Make sure you have the following installed before you begin:
- Python 3.8+ — Download from python.org.
- Streamlit — Install with pip:
pip install streamlit
Step 1: Create the Python Script
Create a new file called age_calculator_app.py and paste in the following code:
import streamlit as st
from datetime import datetime, date
st.set_page_config(page_title="Age Calculator", layout="centered", page_icon="🎂")
st.title("🎉 Age Calculator App 🎂")
st.sidebar.header("📅 Enter Your Birthdate")
dob = st.sidebar.date_input(
"Select your date of birth:",
value=datetime(2000, 1, 1),
min_value=datetime(1900, 1, 1),
max_value=datetime.now(),
)
def calculate_age(birthdate):
today = date.today()
years = today.year - birthdate.year
months = today.month - birthdate.month
days = today.day - birthdate.day
if days < 0:
months -= 1
days += 30
if months < 0:
years -= 1
months += 12
total_days = (today - birthdate).days
return years, months, days, total_days
years, months, days, total_days = calculate_age(dob)
col1, col2, col3 = st.columns(3)
col1.metric("Years", years)
col2.metric("Months", months)
col3.metric("Days", days)
st.info(f"You have been alive for **{total_days:,} days**! 🎉")
Step 2: Run the App
Open your terminal, navigate to the folder where you saved the file, and run:
streamlit run age_calculator_app.py
Step 3: Use the App
The app will open in your browser automatically. Select your date of birth from the sidebar and the app instantly displays your age in years, months, and days, plus a fun total-days count.
How It Works
The age calculator is a great example of how Streamlit turns a plain Python script into an interactive web app. Everything revolves around the sidebar widgets: st.sidebar.date_input() renders a real calendar picker in the browser and returns a Python date object. Every time the user picks a new date, Streamlit re-runs your entire script from top to bottom — this is called the rerun model, and understanding it is the key to building anything in Streamlit.
The math itself lives in the calculate_age() function. Subtracting years, months, and days sounds trivial, but borrowing is the tricky part: if today’s day-of-month is smaller than the birth day, you borrow days from the previous month (and a month from the year if needed) — exactly like subtraction in school, but with base-30/12 units. The total_days figure sidesteps all of that by using date subtraction, which Python handles natively.
Finally, st.columns(3) splits the results into a responsive grid and st.metric() renders each number as a card with a label — the same component used in the Pandas data dashboard.
Key Concepts
st.date_input()— Renders a calendar date picker widget.calculate_age()— Custom function that computes years, months, days, and total days.st.columns()— Splits the page into a clean 3-column grid.st.metric()— Renders each number in a styled metric card.
What to Try Next
- Add a zodiac sign display based on the birth date.
- Show a countdown to the user’s next birthday.
- Add a download button to save the result using
st.download_button().
Common Errors & Fixes
-
ValueError: Cannot compare datetime.datetime to datetime.date —
st.date_input()returns adate, butdatetime.now()is adatetime. Fix by comparing like types: usedate.today()instead ofdatetime.now(). -
Age off by one near a birthday — the borrowing logic must run before the month check. If days go negative, decrement months first, then re-check months before decrementing years.
-
App reloads and resets the picker — you passed a fixed
value=so every rerun resets it. Store the selection inst.session_stateif you want it to persist across interactions. -
st.metricshows decimals — make sureyears,months,daysareint, notfloat, before rendering.
FAQ
How accurate is the days-alive count?
It is exact. Python’s date subtraction accounts for leap years automatically, so the total-days figure is correct to the day.
Can I calculate age in months only?
Yes — total_months = years * 12 + months after the borrowing logic runs. Add it as a fourth st.metric() column.
Does this work with time zones?
For birthdays, time zones rarely matter. If you need them, use the zoneinfo module (built into Python 3.9+) and compute ‘today’ in the user’s zone.