Spaces:
Sleeping
Sleeping
import streamlit as st | |
from datetime import datetime | |
from dateutil.relativedelta import relativedelta | |
# App Title and Description | |
st.title("π Advanced Age Calculator") | |
st.write(""" | |
Calculate your age in years, months, and days! | |
See your next 5 birthdays and get a detailed textual output in English. | |
""") | |
# Input Section with a minimum date of January 1, 1950 | |
dob = st.date_input("Select Your Date of Birth (YYYY-MM-DD):", min_value=datetime(1950, 1, 1)) | |
# Button to trigger calculation | |
if st.button("Calculate Age and Next Birthdays"): | |
if dob: | |
today = datetime.now() | |
dob_datetime = datetime.combine(dob, datetime.min.time()) | |
# Calculate age in years, months, and days | |
age_years = today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)) | |
age_months = relativedelta(today, dob_datetime).months | |
age_days = (today - dob_datetime).days % 30 # Approximation for days in the current month | |
# Calculate next 5 birthdays | |
next_birthdays = [] | |
next_birthday = datetime(today.year, dob.month, dob.day) | |
if next_birthday < today: | |
next_birthday = datetime(today.year + 1, dob.month, dob.day) | |
for _ in range(5): | |
next_birthdays.append(next_birthday.strftime("%A, %d %B %Y")) | |
next_birthday = next_birthday.replace(year=next_birthday.year + 1) | |
# Display Age | |
st.subheader("π Your Age:") | |
st.write(f"**Years:** {age_years}") | |
st.write(f"**Months:** {age_months}") | |
st.write(f"**Days:** {age_days}") | |
# Display Next 5 Birthdays | |
st.subheader("π Next 5 Birthdays:") | |
for idx, birthday in enumerate(next_birthdays, start=1): | |
st.write(f"{idx}. {birthday}") | |
# English Textual Output | |
st.subheader("π Age in English:") | |
st.write(f"You are **{age_years} years, {age_months} months, and {age_days} days old**.") | |
st.write("Your next five birthdays will be on the dates listed above.") | |
else: | |
st.write("Please select a valid date of birth.") | |