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()