Age-calculator / app.py
zainabbbbbbbbbb's picture
Create app.py
2ca6e11 verified
import streamlit as st
from datetime import datetime
# Function to calculate age
def calculate_age(birthdate):
# Get today's date
today = datetime.today()
# Calculate the difference in years, months, and days
years = today.year - birthdate.year
months = today.month - birthdate.month
days = today.day - birthdate.day
# Adjust months and years if needed
if months < 0:
months += 12
years -= 1
if days < 0:
# Calculate the previous month
previous_month = today.replace(year=today.year, month=today.month - 1) if today.month > 1 else today.replace(year=today.year - 1, month=12)
days += (previous_month.replace(day=1) - today.replace(day=1)).days
# Calculate the total number of minutes
total_minutes = (today - birthdate).total_seconds() / 60
return years, months, days, int(total_minutes)
# Streamlit UI
st.title("Age Calculator")
# User input for birthdate
birthdate_input = st.date_input("Enter your birthdate:")
# Calculate age when user submits
if st.button("Calculate Age"):
if birthdate_input:
# Convert date input into datetime object
birthdate = datetime.strptime(str(birthdate_input), "%Y-%m-%d")
# Calculate age and minutes
years, months, days, total_minutes = calculate_age(birthdate)
# Display results
st.write(f"Your age is {years} years, {months} months, {days} days.")
st.write(f"Your age in minutes is: {total_minutes} minutes.")
else:
st.write("Please enter a valid birthdate.")