File size: 1,566 Bytes
2ca6e11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from datetime import datetime

# Function to calculate age
def calculate_age(birthdate):
    # Get today's date
    today = datetime.today()

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

    # Adjust months and years if needed
    if months < 0:
        months += 12
        years -= 1

    if days < 0:
        # Calculate the previous month
        previous_month = today.replace(year=today.year, month=today.month - 1) if today.month > 1 else today.replace(year=today.year - 1, month=12)
        days += (previous_month.replace(day=1) - today.replace(day=1)).days

    # Calculate the total number of minutes
    total_minutes = (today - birthdate).total_seconds() / 60

    return years, months, days, int(total_minutes)

# Streamlit UI
st.title("Age Calculator")

# User input for birthdate
birthdate_input = st.date_input("Enter your birthdate:")

# Calculate age when user submits
if st.button("Calculate Age"):
    if birthdate_input:
        # Convert date input into datetime object
        birthdate = datetime.strptime(str(birthdate_input), "%Y-%m-%d")

        # Calculate age and minutes
        years, months, days, total_minutes = calculate_age(birthdate)

        # Display results
        st.write(f"Your age is {years} years, {months} months, {days} days.")
        st.write(f"Your age in minutes is: {total_minutes} minutes.")
    else:
        st.write("Please enter a valid birthdate.")