Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,47 +1,55 @@
|
|
| 1 |
-
import
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
try:
|
| 6 |
-
# Convert the
|
| 7 |
-
dob_date = datetime.strptime(
|
| 8 |
today = datetime.today()
|
| 9 |
-
|
| 10 |
-
# Calculate age in years, months, and days
|
| 11 |
years = today.year - dob_date.year
|
| 12 |
months = today.month - dob_date.month
|
| 13 |
days = today.day - dob_date.day
|
| 14 |
-
|
| 15 |
# Adjust months and days if necessary
|
| 16 |
if days < 0:
|
| 17 |
months -= 1
|
| 18 |
-
days += 30 # Approximate days in a month
|
| 19 |
if months < 0:
|
| 20 |
years -= 1
|
| 21 |
months += 12
|
| 22 |
-
|
| 23 |
-
# Calculate time remaining for next birthday
|
| 24 |
next_birthday = datetime(today.year, dob_date.month, dob_date.day)
|
| 25 |
-
if today > next_birthday: # If birthday
|
| 26 |
next_birthday = datetime(today.year + 1, dob_date.month, dob_date.day)
|
| 27 |
remaining_days = (next_birthday - today).days
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
age_message = f"You are {years} years, {months} months, and {days} days old."
|
| 31 |
remaining_message = f"Your next birthday is in {remaining_days} days."
|
| 32 |
-
|
| 33 |
return age_message, remaining_message
|
| 34 |
-
|
| 35 |
except ValueError:
|
| 36 |
-
return "Invalid date format. Please use
|
| 37 |
-
|
| 38 |
-
#
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
from datetime import datetime
|
| 3 |
|
| 4 |
+
# Function to calculate the age and the time remaining for the next birthday
|
| 5 |
+
def calculate_age_and_birthday(birthday: str):
|
| 6 |
try:
|
| 7 |
+
# Convert the string input into a datetime object
|
| 8 |
+
dob_date = datetime.strptime(birthday, "%m/%d/%Y")
|
| 9 |
today = datetime.today()
|
| 10 |
+
|
| 11 |
+
# Calculate the age in years, months, and days
|
| 12 |
years = today.year - dob_date.year
|
| 13 |
months = today.month - dob_date.month
|
| 14 |
days = today.day - dob_date.day
|
| 15 |
+
|
| 16 |
# Adjust months and days if necessary
|
| 17 |
if days < 0:
|
| 18 |
months -= 1
|
| 19 |
+
days += 30 # Approximate number of days in a month
|
| 20 |
if months < 0:
|
| 21 |
years -= 1
|
| 22 |
months += 12
|
| 23 |
+
|
| 24 |
+
# Calculate the time remaining for the next birthday
|
| 25 |
next_birthday = datetime(today.year, dob_date.month, dob_date.day)
|
| 26 |
+
if today > next_birthday: # If the birthday has passed this year
|
| 27 |
next_birthday = datetime(today.year + 1, dob_date.month, dob_date.day)
|
| 28 |
remaining_days = (next_birthday - today).days
|
| 29 |
+
|
| 30 |
+
# Return the results
|
| 31 |
age_message = f"You are {years} years, {months} months, and {days} days old."
|
| 32 |
remaining_message = f"Your next birthday is in {remaining_days} days."
|
| 33 |
+
|
| 34 |
return age_message, remaining_message
|
| 35 |
+
|
| 36 |
except ValueError:
|
| 37 |
+
return "Invalid date format. Please use MM/DD/YYYY.", ""
|
| 38 |
+
|
| 39 |
+
# Streamlit app interface
|
| 40 |
+
def main():
|
| 41 |
+
st.title("Age Calculator")
|
| 42 |
+
|
| 43 |
+
# Input: User provides their date of birth in MM/DD/YYYY format
|
| 44 |
+
dob = st.text_input("Enter your Date of Birth (MM/DD/YYYY)", "")
|
| 45 |
+
|
| 46 |
+
if dob:
|
| 47 |
+
age_message, remaining_message = calculate_age_and_birthday(dob)
|
| 48 |
+
|
| 49 |
+
# Display the results
|
| 50 |
+
st.write(age_message)
|
| 51 |
+
st.write(remaining_message)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
main()
|
| 55 |
+
|