File size: 1,273 Bytes
8ca677c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from datetime import datetime

# Define a function for calculating age
def calculate_age(birthdate):
    today = datetime.today()
    age_years = today.year - birthdate.year
    age_months = today.month - birthdate.month
    age_days = today.day - birthdate.day
    
    if age_months < 0:
        age_years -= 1
        age_months += 12
        
    if age_days < 0:
        age_months -= 1
        age_days += 30  # Approximate value for simplicity

    return age_years, age_months, age_days

# Streamlit app interface
def age_calculator():
    st.title("Age Calculator")
    st.write("Enter your birthdate to calculate your age in years, months, and days.")
    
    # Date input from the user
    birthdate = st.date_input("Select your birthdate", min_value=datetime(1900, 1, 1), max_value=datetime.today())
    
    # Calculate the age when the button is pressed
    if st.button("Calculate Age"):
        if birthdate:
            age_years, age_months, age_days = calculate_age(birthdate)
            st.write(f"You are {age_years} years, {age_months} months, and {age_days} days old.")
        else:
            st.write("Please select a valid birthdate.")

# Call the age calculator function
if __name__ == "__main__":
    age_calculator()