# How to calculate age in days from date of birth in Python

This Python tutorial, going to be very interesting as we will learn how to calculate age in days from the date of birth in Python. Just imagine that you know someone’s date of birth and you can create a Python program to know his age in days.

In order to build this program you will need to use the following module:

• datetime module

From this module, we have to import these following two classes:

• datetime
• timedelta

## Python Program to calculate age in days from date of birth

```from datetime import datetime, timedelta
particular_date = datetime(1996, 1, 1)
new_date = datetime.today() - particular_date
print (new_date.days)```

Output:

```\$ python codespeedy.py
8496```

Here we got our result. The age of the person is 8496 days, whose DOB is 1st of January, 1996.

Explanation of this program

Firstly we have imported datetime and timedelta classes from datetime module by the below line of code.

`from datetime import datetime, timedelta`

datetime module has many classes that can be used to manipulate date and time both in simple as well as complex ways. To read documentation please refer to this link: https://docs.python.org/2/library/datetime.html

`particular_date = datetime(1996, 1, 1)`

We have stored the date of birth of a person using datetime() in variable particular_date.

The format of the date is year month date here.

datetime.today() returns the current date/system date.

Then we have used “-” operator to subtract date of birth from the current date to get the age from date of birth.

```new_date = datetime.today() - particular_date
print (new_date.days)```

Point to be noted:

If we use print(new_date)

It will give us the output like

`8496 days, 12:40:31.093823`

So in order to remove the time from days, we have used  new_date.days.

It will return only days.

### Calculate age from DOB with days, hours, minutes and seconds

```from datetime import datetime, timedelta
particular_date = datetime(1996, 1, 1)
new_date = datetime.today() - particular_date
print (new_date)```

It will give you the output with days and exact time in microseconds.

Run it on your machine to see the output.

You may also learn, How to subtract days from date in Python

### 2 responses to “How to calculate age in days from date of birth in Python”

1. Pervaiz Iqbal says:

Hi I need your help in Python to write Name, Date of Birth and Place of Birth. plase send me the codes

2. julio says:

Write a simple Python program that prompts the user for the year he or she was born. Using only the birth year, echo back to the user how old he or she is now and how old the user will be in the year 2035.