In this post, i am going to write python program to calculate age in years,months and days. Age calculator is an application that computes age in terms of years ,months and days based on your birthdate.

In this ,user has to input his/her birthdate ,this application gives actual age as an output. In order to calculate age ,we need two dates : 1) current date (todays date) 2) Birthdate

Python provides datetime module to perform different types of operation on date and time. I will use datetime module to code this programs. Lets see how to include datetime module in program and how to use different methods and function of datetime module to calculate age in years,months and days successfully.

How to include datetime module in python code

To include datetime module in python program, we need to write import keyword alongwith modulename as shown below

import datetime

Now,we can access all methods and functions of datetime module.

Then we ask user to enter his/her birthdate by using input() . I will use strptime() method of datetime module to fromat date in desired format. It will creates date object.

We will extract year,month and date from birthdate which is in yyyy/mm/dd format

birthdate = input("Enter your birthdate :")
my_date = datetime.strptime(birthdate, "%Y-%m-%d")
b_year = my_date.year
b_month = my_date.month
b_date =

Here, my_date.year,my_date.month and extract years from birthdate and birth_year in variable b_year,months and date from birth date and store separately in different variable say b_month and b_date respectively.

Similarly we will find out todays date and time by using now() function of datetime module and extract year,month and date from current date ;store them in different variables like below.

# current date and time
now =

# get year from date
c_year = int(now.strftime("%Y"))

# get month from date
c_month = int(now.strftime("%m"))

# get day from date
c_date =int( now.strftime("%d"))

Then we will create user defined function to calculate age in moths,years and days like below

def ageCalculator(years, months, days,year,month,date):
    import datetime
    today =,months,days)
    dob =, month, date)
    years= ((today-dob).total_seconds()/ (365.242*24*3600))
    print('You are {0} years, {1} months, {2} days old.'.format(yearsInt,monthsInt,daysInt))

Complete source code of project

Source code for age calculator in python


Python projects

Password picker

Password validation system


Leave a Reply

Your email address will not be published.