Lesson 1 of 0
In Progress

Calculating Age in Years

Kedeisha June 16, 2023

Write a Python function that accepts a string representing a date of birth and calculates the age in years as of today’s date. Assume the date string is in the format ‘YYYY-MM-DD’.

Input Data: Your function should accept a single string representing a date.

Explanation of Solution: The function calculate_age uses strptime to convert the date string to a datetime object. It then calculates the age by subtracting the birth year from the current year. If the current month and day is before the birth month and day, it subtracts 1 from the age, as the birthday hasn’t been reached in the current year yet.