To calculate someone's age, you need their date of birth and the current date. Then, you subtract the birth date from the current date to determine the age. Here's how you can do it:
- Get the Birth Date: This is the date the person was born.
- Get the Current Date: This is the present date.
- Calculate the Age:
- Subtract the birth year from the current year.
- If the birth month is after the current month, or if the birth month is the same as the current month but the birth day is after the current day, subtract one from the age (because the birthday hasn't occurred yet this year).
Here's a simple way to do it in Python-
from datetime import datetime
def calculate_age(birth_date):
current_date = datetime.now()
age = current_date.year - birth_date.year - ((current_date.month, current_date.day) < (birth_date.month, birth_date.day))
return age
Example usage:
Assume birth_date is in the format YYYY-MM-DD
birth_date = datetime(1990, 5, 15) Example birth date
age = calculate_age(birth_date)
print("Age:", age)
Replace birth_date
with the person's actual birth date in the format datetime(year, month, day)
. This function will return the age of the person in years.