Spaces:
Sleeping
Sleeping
Download app.py from SyedaRidaAI/Age_calculator: direct link, hf CLI and curl.
- Browser
- Download file 1.89 kB
-
https://huggingface.co/spaces/SyedaRidaAI/Age_calculator/resolve/main/app.py
- Command line
-
hf download hf://spaces/SyedaRidaAI/Age_calculator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SyedaRidaAI/Age_calculator/resolve/main/app.py
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')}.") | |