Age calculator
from datetime import datetime
def calculate_age(birthdate):
today = datetime.today()
birth_date = datetime.strptime(birthdate, "%Y-%m-%d")
age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
return age
# Get user input
birthdate = input("Enter your birthdate (YYYY-MM-DD): ")
try:
age = calculate_age(birthdate)
print(f"Your age is: {age} years")
except ValueError:
print("Invalid date format. Please enter the date in YYYY-MM-DD format.")
Comments
Post a Comment