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
Prerequisites
Make sure you have the following installed before you begin:
pip install streamlitStep 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.pyStep 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
What to Try Next
st.download_button().Common Errors & Fixes
st.date_input() returns a date, but datetime.now() is a datetime. Fix by comparing like types: use date.today() instead of datetime.now().value= so every rerun resets it. Store the selection in st.session_state if you want it to persist across interactions.years, months, days are int, not float, 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.