Python Program to Calculate Age in Years Months and Days

python program to calculate age in years, months, and days

Introduction

In this tutorial, we will create a program to calculate age in years, months, and days in python. The program will take two inputs from the user, the date of birth and the current date

It will calculate someone's age most accurately. For example, if the given year of birth is a leap year and the birthday is 29th February and the current year is not a leap year or a leap year, the program will calculate the age correctly for any conditions.

There is a python module called datetime that offers several functions to deal with dates and times. I used this module here to create this age calculator program in python.

Visit AlsoPython Keylogger - Build a Simple Keylogger in Python

Example

Date Format: dd/mm/yyyy
Please Enter Your Birth Date: 29/02/2000
Please Enter The Current Date: 21/05/2021
YOUR Age is:  21 years  2 months  22 days

Full Code

Here, I've declared one single comment line for each segment of the code. It'll will help you to understand the objectives of the each segment of the program.


'''Age calculator in Python'''
from datetime import date

# Months store total #days of each month.
Months = {
1:31, 2:28, 3:31, 4:30, 5:31, 6:30, 7:31,
8:31, 9:30, 10:31, 11:30, 12:31
}

# Function that check if a year is leap year or not
def IsLeapYear(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False

# Returns differnce between two dates in days
def NumOfDays(date1, date2):
return (date2 - date1).days

print("Date Format: dd/mm/yyyy")
day1, mon1, year1=input("Please Enter Your Birth Date: ").split('/')
day2, mon2, year2=input("Please Enter The Current Date: ").split('/')

# Converting given dates into date format
date1 = date(int(year1), int(mon1), int(day1))
date2 = date(int(year2), int(mon2), int(day2))


# Return value from the function - NumOfDays
TotalDays = NumOfDays(date1, date2)


# If Birth year and the current year is same
if int(year1) == int(year2):
month = TotalDays/30
day = TotalDays%30
year = 0
else:
year = TotalDays/365
month = (TotalDays%365)/30

# Check if the given year is a leap year or not.
# If Yes, then Make the total number of days in the month of
# February 29 instead of 28.
if IsLeapYear(int(year2)):
Months[2] = 29

if int(day2) >= int(day1):
day = int(day2) - int(day1)
# If the current month is February and the current year is leap
# year or not
elif int(mon2) == 2 and (IsLeapYear(int(year2)) or (not IsLeapYear(int(year2)))):
year = year
month = 11
# Check is the current month is Jan. or Not
if int(mon2) == 1:
prevMonth = Months[int(mon2)]
else:
prevMonth = Months[int(mon2)-1]
days = prevMonth - int(day1) + int(day2)
day = days
else:
if int(mon2) == 1:
prevMonth = Months[int(mon2)]
else:
prevMonth = Months[int(mon2)-1]
days = prevMonth - int(day1) + int(day2)
day = days
month = month


print("YOUR Age is: ",int(year),"years ", int(month), "months ", int(day), "days")

The Output

Date Format: dd/mm/yyyy
Please Enter Your Birth Date: 29/02/2000
Please Enter The Current Date: 08/03/2022
YOUR Age is:  22 years  0 months  8 days

Download The Code

Visit AlsoSend Secret Messages to Your Friends using Python

Summary

In this tutorial, we create a python program to calculate someone's age in years, months, and days format. The program takes two inputs from the users: the birth date(dd/mm/yy) and the current date(dd/mm/yy). As a result the program displays the age of that person on the present day.

Using the same logic I've built an Age Calculator Application using Python Tkinter library. Check it out also from there.

So, find your current age using this python program. For any query related to this topic, please leave your comment below. You'll get the answer shortly.

Thanks for reading!💙

PySeek

Subhankar Rakshit

Meet Subhankar Rakshit, a Computer Science postgraduate (M.Sc.) and the creator of PySeek. Subhankar is a programmer, specializes in Python language. With a several years of experience under his belt, he has developed a deep understanding of software development. He enjoys writing blogs on various topics related to Computer Science, Python Programming, and Software Development.

Post a Comment (0)
Previous Post Next Post