# 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:

```.wp-block-code{border:0;padding:0}.wp-block-code>div{overflow:auto}.shcb-language{border:0;clip:rect(1px, 1px, 1px, 1px);-webkit-clip-path:inset(50%);clip-path:inset(50%);height:1px;margin:-1px;overflow:hidden;padding:0;position:absolute;width:1px;word-wrap:normal;word-break:normal}.hljs{box-sizing:border-box}.hljs.shcb-code-table{display:table;width:100%}.hljs.shcb-code-table>.shcb-loc{color:inherit;display:table-row;width:100%}.hljs.shcb-code-table .shcb-loc>span{display:table-cell}.wp-block-code code.hljs:not(.shcb-wrap-lines){white-space:pre}.wp-block-code code.hljs.shcb-wrap-lines{white-space:pre-wrap}.hljs.shcb-line-numbers{border-spacing:0;counter-reset:line}.hljs.shcb-line-numbers>.shcb-loc{counter-increment:line}.hljs.shcb-line-numbers .shcb-loc>span{padding-left:0.75em}.hljs.shcb-line-numbers .shcb-loc::before{border-right:1px solid #ddd;content:counter(line);display:table-cell;padding:0
0.75em;text-align:right;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;white-space:nowrap;width:1%}```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}.")

# 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)

# 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)

# 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)

# 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)

# 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.