Spaces:
Sleeping

Age_calculator / app.py
SyedaRidaAI's picture
Update app.py
0158211 verified
Raw History Blame Contribute Delete
1.89 kB
import streamlit as st
from datetime import date
from calendar import monthrange
# Set up the page title and layout
st.set_page_config(page_title="๐Ÿ“… Future Age Predictor", layout="centered")
st.title("๐ŸŽ‚ Age Calculator and Future Age Predictor")
# Step 1: Ask user for the birthdate
st.subheader("๐Ÿ‘ถ Enter Birthdate")
birth_date = st.date_input(
"๐Ÿ“… Select birthdate:",
min_value=date(1000, 1, 1) # Allow very old dates
)
# Step 2: Ask user the date they want to know the age at (can be future or past)
st.subheader("๐Ÿ”ฎ Enter the target date (when you want to calculate age)")
target_date = st.date_input(
"๐Ÿ“† Select the target date:",
value=date.today(), # Default is today
min_value=date(1000, 1, 1) # Allow long past dates
)
# Step 3: Function to calculate age in years, months, days
def calculate_age_between_dates(start_date, end_date):
years = end_date.year - start_date.year
months = end_date.month - start_date.month
days = end_date.day - start_date.day
# Adjust for negative days (borrow days from previous month)
if days < 0:
months -= 1
prev_month = end_date.month - 1 or 12
prev_year = end_date.year if end_date.month > 1 else end_date.year - 1
days += monthrange(prev_year, prev_month)[1]
# Adjust for negative months (borrow from year)
if months < 0:
years -= 1
months += 12
return years, months, days
# Step 4: Show result when user clicks button
if st.button("๐Ÿ“ Calculate Age"):
if target_date < birth_date:
st.warning("โš ๏ธ Target date is before birth! Please select a valid future date.")
else:
years, months, days = calculate_age_between_dates(birth_date, target_date)
st.success(f"๐ŸŽ‰ The person will be {years} years, {months} months, and {days} days old on {target_date.strftime('%d %B %Y')}.")