Simple Age Calculator Using Python
- Given birth date in y/m/d format, write a Python program to find the present age in years.
- Calculate your age with this Program.
Source Code:
#Follow @Programmerfect
from datetime import date
def calculateAge(born):
today = date.today()
try:
birthday = born.replace(year = today.year)
#raised when birth date is february 29
#and the currebt year is not a leap year
except ValueError:
birthday = born.replace(year = today.year,
month = born.month + 1, day = 1)
if birthday > today:
return today.year - born.year -1
else:
return today.year - born.year
#Driver Code
print(calculateAge(date(2000,3,16)),"years")
from datetime import date
def calculateAge(born):
today = date.today()
try:
birthday = born.replace(year = today.year)
#raised when birth date is february 29
#and the currebt year is not a leap year
except ValueError:
birthday = born.replace(year = today.year,
month = born.month + 1, day = 1)
if birthday > today:
return today.year - born.year -1
else:
return today.year - born.year
#Driver Code
print(calculateAge(date(2000,3,16)),"years")