age_calculator / app.py
Dearsawan's picture
Update app.py
7a0053b verified
import streamlit as st
from datetime import datetime
# Define a function for calculating age
def calculate_age(birthdate):
today = datetime.today()
age_years = today.year - birthdate.year
age_months = today.month - birthdate.month
age_days = today.day - birthdate.day
if age_months < 0:
age_years -= 1
age_months += 12
if age_days < 0:
age_months -= 1
age_days += 30 # Approximate value for simplicity
return age_years, age_months, age_days
# Streamlit app interface
def age_calculator():
st.title("Age Calculator")
st.write("Enter your birthdate to calculate your age in years, months, and days.")
# Date input from the user
birthdate = st.date_input("Select your birthdate", min_value=datetime(1900, 1, 1), max_value=datetime.today())
# Calculate the age when the button is pressed
if st.button("Calculate Age"):
if birthdate:
age_years, age_months, age_days = calculate_age(birthdate)
st.write(f"You are {age_years} years, {age_months} months, and {age_days} days old.")
else:
st.write("Please select a valid birthdate.")
# Call the age calculator function
if __name__ == "__main__":
age_calculator()