DevelopmentJanuary 09, 20255 min read

How to Build an Age Calculator App Using Python and Streamlit

Learn how to create an interactive Age Calculator web app with Python and Streamlit that calculates your exact age in years, months, and days with fun insights.

Galvan

Galvan

Founder & Creator

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.

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:
  • code
    pip install streamlit

    Step 1: Create the Python Script

    Create a new file called age_calculator_app.py and paste in the following code:

    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:

    code
    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.

    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().