Program in python for calculating date of birth

 Here is a program in Python that calculates a person's age based on their date of birth:



from datetime import datetime


def calculate_age(born):

    today = datetime.today()

    return today.year - born.year - ((today.month, today.day) < (born.month, born.day))


# Prompt the user for their date of birth

print("Enter your date of birth (YYYY-MM-DD): ")

dob_input = input()


# Parse the user's input into a date object

dob = datetime.strptime(dob_input, "%Y-%m-%d")


# Calculate the user's age based on their date of birth

age = calculate_age(dob)


# Print the user's age

print(f"Your age is: {age}")



This program first prompts the user to enter their date of birth in the format YYYY-MM-DD. It then uses the datetime module to parse the user's input into a datetime object. The calculate_age function is then called, passing in the datetime object as an argument. This function calculates the user's age by subtracting their date of birth from the current date, and taking into account the possibility of a birthday having occurred in the current year. The result is then printed to the screen.

Comments