File size: 1,859 Bytes
042b965
adee1b3
 
042b965
 
adee1b3
042b965
 
adee1b3
042b965
 
adee1b3
 
 
042b965
adee1b3
 
 
042b965
adee1b3
 
 
042b965
 
adee1b3
042b965
adee1b3
 
042b965
 
adee1b3
 
042b965
adee1b3
042b965
adee1b3
042b965
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import streamlit as st
from datetime import datetime

# Function to calculate the age and the time remaining for the next birthday
def calculate_age_and_birthday(birthday: str):
    try:
        # Convert the string input into a datetime object
        dob_date = datetime.strptime(birthday, "%m/%d/%Y")
        today = datetime.today()

        # Calculate the age in years, months, and days
        years = today.year - dob_date.year
        months = today.month - dob_date.month
        days = today.day - dob_date.day

        # Adjust months and days if necessary
        if days < 0:
            months -= 1
            days += 30  # Approximate number of days in a month
        if months < 0:
            years -= 1
            months += 12

        # Calculate the time remaining for the next birthday
        next_birthday = datetime(today.year, dob_date.month, dob_date.day)
        if today > next_birthday:  # If the birthday has passed this year
            next_birthday = datetime(today.year + 1, dob_date.month, dob_date.day)
        remaining_days = (next_birthday - today).days

        # Return the results
        age_message = f"You are {years} years, {months} months, and {days} days old."
        remaining_message = f"Your next birthday is in {remaining_days} days."

        return age_message, remaining_message

    except ValueError:
        return "Invalid date format. Please use MM/DD/YYYY.", ""

# Streamlit app interface
def main():
    st.title("Age Calculator")

    # Input: User provides their date of birth in MM/DD/YYYY format
    dob = st.text_input("Enter your Date of Birth (MM/DD/YYYY)", "")

    if dob:
        age_message, remaining_message = calculate_age_and_birthday(dob)

        # Display the results
        st.write(age_message)
        st.write(remaining_message)

if __name__ == "__main__":
    main()