
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculate Age in Years using Python
Generally, the age of a person is calculated by subtracting the birth year with the current year then the age of the person will be generated in years. In the same way we can calculate the age of a person by using the python modules datetime, dateutil and timedelta.
General Calculation of Age in years
Current year - Birth year
Example
In this example we are implementing the general way of calculating the age using pythn language by passing the current year and birth years as the inputs to created function age_calculator() then the total age will be generated in years.
def age_calculator(current_year,birth_year): age = current_year - birth_year print("The age of the person in years is",age,"years") age_calculator(2023,1995)
Output
The age of the person in years is 28 years
There are some methods available in python which helps us to calculate the age of a person in years. Let's see those methods one by one in detail.
Using datetime module
In python we have the module called datetime to get the current date to subtract birth year of the person. The date.today() is the function available in datetime module which gets the present date.
Example
In this example we are passing the birth date to the split function to get birth year and using the date.today() function generated the current date then subtracted the birth year from the current year.
import datetime birthdate = "14-05-1991" day,month,year = map(int, birthdate.split("-")) today = datetime.date.today() age = today.year - year - ((today.month, today.day) < (month, day)) print("Your age is",age,"years.")
Output
Your age is 32 years.
Using dateutil method
The other module available in python is dateutil, which provides a relativedelta() function to calculate the difference between the birth date and the current date.
The relativedelta() function of the dateutil module takes the current year and birth year as the input arguments and returns the age as the output.
Example
In this example we are taking the birth date as the input and separating the birth year using the datetime.datetime.strptime() function. Next generated the current year using the datetime.date.today() function then subtracted the birth year from the current year to generate the persons age.
from dateutil.relativedelta import relativedelta import datetime birthdate = "22-01-1995" birthdate = datetime.datetime.strptime(birthdate, '%d-%m-%Y').date() today = datetime.date.today() age = relativedelta(today, birthdate).years print("your age is",age,"years.")
Output
your age is 28 years.
Using timedelta() function
The timedelta() function from the datetime module is used to calculate the number of days between two dates and then divides the average number of days in a year (365.2425) to get the age in years.
Example
Here in this example we are generating the current year by using the datetime.date.today() and calculated the difference between the current date, the birth date and then divides the average number of days in a year and returns the age of the person.
import datetime def age_calculator(birthdate): today = datetime.date.today() age = (today - birthdate) // datetime.timedelta(days=365.2425) return age birthdate = datetime.date(1991, 5, 14) age = age_calculator(birthdate) print("The age of the person in years is",age,"years")
Output
The age of the person in years is 32 years