Dearsawan commited on
Commit
8ca677c
·
verified ·
1 Parent(s): d0de896

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from datetime import datetime
3
+
4
+ # Define a function for calculating age
5
+ def calculate_age(birthdate):
6
+ today = datetime.today()
7
+ age_years = today.year - birthdate.year
8
+ age_months = today.month - birthdate.month
9
+ age_days = today.day - birthdate.day
10
+
11
+ if age_months < 0:
12
+ age_years -= 1
13
+ age_months += 12
14
+
15
+ if age_days < 0:
16
+ age_months -= 1
17
+ age_days += 30 # Approximate value for simplicity
18
+
19
+ return age_years, age_months, age_days
20
+
21
+ # Streamlit app interface
22
+ def age_calculator():
23
+ st.title("Age Calculator")
24
+ st.write("Enter your birthdate to calculate your age in years, months, and days.")
25
+
26
+ # Date input from the user
27
+ birthdate = st.date_input("Select your birthdate", min_value=datetime(1900, 1, 1), max_value=datetime.today())
28
+
29
+ # Calculate the age when the button is pressed
30
+ if st.button("Calculate Age"):
31
+ if birthdate:
32
+ age_years, age_months, age_days = calculate_age(birthdate)
33
+ st.write(f"You are {age_years} years, {age_months} months, and {age_days} days old.")
34
+ else:
35
+ st.write("Please select a valid birthdate.")
36
+
37
+ # Call the age calculator function
38
+ if __name__ == "__main__":
39
+ age_calculator()