Calculate age from date of birth using Python


There are many ways to calculate the age from a birthdate in Python. One common method is to use the built-in datetime module. This module provides various functions and classes for working with dates and times, making it easy to calculate the age from a given birthdate.

Calculate age from date of birth using Python

To calculate the age from a birthdate in Python, you first need to import the datetime module. You can then use the datetime.today() function to get the current date, and the datetime.timedelta() function to calculate the difference between the two dates. To get the number of years between the two dates, you can simply divide the total number of days by 365. For example, if someone was born on January 1st, 1980 and today is January 1st, 2020, their age would be 40 years:

import datetime today = datetime.today() birthday = datetime.date(1980, 1, 1) age = (today - birthday).days / 365 print(age) # prints 40.0
Code language: PHP (php)

As you can see, calculating the age from a birthdate in Python is relatively straightforward. However, keep in mind that this method will only be accurate if someone was born on January 1st. For people born on other days of the year, you’ll need to adjust the calculation accordingly.

Python Tkinter: GUI Age Calculator

# import libraries from tkinter import * from datetime import date # initialized window root = Tk() root.geometry('280x300') root.resizable(0, 0) root.title('Age Calculator') statement = Label(root) # defining the function for calculating age def ageCalc(): global statement statement.destroy() today = date.today() birthDate = date(int(yearEntry.get()), int( monthEntry.get()), int(dayEntry.get())) age = today.year - birthDate.year if today.month < birthDate.month or today.month == birthDate.month and today.day < birthDate.day: age -= 1 statement = Label(text=f"{nameValue.get()}'s age is {age}.") statement.grid(row=6, column=1, pady=15) # creating a label for person's name to display l1 = Label(text="Name: ") l1.grid(row=1, column=0) nameValue = StringVar() # creating a entry box for input nameEntry = Entry(root, textvariable=nameValue) nameEntry.grid(row=1, column=1, padx=10, pady=10) # label for year in which user was born l2 = Label(text="Year: ") l2.grid(row=2, column=0) yearValue = StringVar() yearEntry = Entry(root, textvariable=yearValue) yearEntry.grid(row=2, column=1, padx=10, pady=10) # label for month in which user was born l3 = Label(text="Month: ") l3.grid(row=3, column=0) monthValue = StringVar() monthEntry = Entry(root, textvariable=monthValue) monthEntry.grid(row=3, column=1, padx=10, pady=10) # label for day/date on which user was born l4 = Label(text="Day: ") l4.grid(row=4, column=0) dayValue = StringVar() dayEntry = Entry(root, textvariable=dayValue) dayEntry.grid(row=4, column=1, padx=10, pady=10) # create a button for calculating age button = Button(text="Calculate age", command=ageCalc) button.grid(row=5, column=1) # infinite loop to run program root.mainloop()
Code language: Python (python)

How to use this Script

In this age calculator app, users can type in their date of birth, and the app will calculate and display their age.

Step 1:

First of all, we need to import two libraries into our code. The first one is the tkinter library. Then, we need the datetime library to work with dates.

Step 2:

Now, let’s create a simple window for our app and name it as Age Calculator.

Step 3:

Then, we are going to create four labels, each for the name, year, month, and the date, and put them in the grid.

We will create entry fields to get the user inputs corresponding to all the labels created. Put them at the right side of the corresponding labels using the grid method.

Step 4:

Then, we are going to define a function, which will calculate the age of the user by subtracting the user’s birth date from today’s date. We name that function as ageCalc().

Then, we create a Label area that will display the age of the user as output in the function itself.

Step 5:

Then, we are going to create a button for users to submit their input values. We link the button to the ageCalc function.

Finally, let’s run everything inside the window using the mainloop() method.

Another popular method is to use the dateutil module. This module provides powerful tools for working with dates and times, including a function for calculating the age from a given birthdate. Whichever method you choose, calculating the age from a birthdate in Python is simple and straightforward.

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts