Write a Python program to calculate an age in year


The program calculates the number of years between two dates, the date of birth (dob) and today's date (today). The dob is set to June 8th, 2014, and the today variable is set to today's date using the date.today() method.

The number of years between dob and today is calculated by subtracting the year of the dob from the year of today and then subtracting the result of an expression that checks if the current month and day are less than the month and day of the dob. The final result is the number of full years between the two dates, rounded down to the nearest integer.

Source Code

from datetime import date
 
dob = date(2014,6,8)
today = date.today()
print(today.year - dob.year - ((today.month, today.day) < (dob.month, dob.day)))

Output

8

Example Programs