 # How to Calculate Time Difference in Python

Often Python developers might need to calculate time difference in python, to use it in their application/website, or simply to measure execution time. In this article, we will learn how to calculate time difference in Python.

## How to Calculate Time Difference in Python

Most developers use the default datetime module that is already pre-installed in python. It allows you to easily create datetime objects required to calculate time difference. datetime module allows you to create date, time, or datetime objects, as per your requirement, to calculate time difference.

Here is a simple example to import datetime module into your code and get a datetime object that points to current time.

```>>> import datetime
>>> a = datetime.datetime.now()
>>> print a
2022-01-31 12:35:37.309000```

Now that you can create a datetime object, here is a simple example to calculate time difference. You just need to store current time in a variable, wait for a while, store current time in another variable, and get the difference between both variables.

```>>> import datetime
>>> a = datetime.datetime.now()
>>> # ...wait a while...
>>> b = datetime.datetime.now()
>>> print(b-a)
0:03:43.984000```

As you can see, datetime returns the time difference in HH:MM:SS format, instead of returning total number of seconds, as done in Linux/MySQL. It also mentions number of days, if any. So this returns a very intuitive time difference value.

If the above code gives an error on your Python(<3), try importing datetime module from datetime package.

`from datetime import datetime`

You can also create datetime, date, time objects by inputting specific values. Here are a few examples to create date, time, datetime objects for calculating time difference.

```# create time object
from datetime import time

# time(HH,MM,SS,MS)
time1 = datetime.time(23,27,45,4600)
print(time1)
# Output: 23:27:45.004600

# create date object
from datetime import date

# date(YYYY,MM,DD)
date1 = datetime.date(2022,1,27)
print(date1)

# Output: 2022-01-27

# create datetime object
# datetime(YYYY,MM,DD,HH,MM,SS,MS)
dt1 = datetime.datetime(2022,1,27,13,27,45,46000)
print(dt1)

# Output: 2022-01-27 13:27:45.046000```

As you can see, you can easily create date, time, datetime objects in python by passing year, month, day, hour, minute, seconds and microsecond information, as required, using date(), time(), datetime() functions respectively. You need to mention year, month and day information for date() function, and hour, minute, second information for time() function.

When you subtract one datetime, date, or time object from another, you get a timedelta object.

```dt1 = datetime.datetime(2022,1,27,13,27,45,46000)
dt2 = datetime.datetime(2022,1,30,14,28)
tdelta = dt2 - dt1
print(tdelta)
print(type(tdelta))

# Output
3 days, 1:00:14.954000
<class 'datetime.timedelta'>```

Timedelta object is useful as it has many pre-built attributes and functions to quickly derive specific information as per your requirement.

For example, as you can see, the timedelta gives time difference in days, hours, minutes, and seconds. If you only want to get number of days in time difference, use the days attribute of timedelta object. Here is an example to get number of days between the above two dates.

```print(tdelta.days)
3 days```

Similarly, if you want time difference in number of seconds, you just need to use seconds attribute of timedelta object. It will convert the time part of your timedelta (hh:mm:ss) into seconds.

```print(tdelta.seconds)
3614```

If you want to get the entire time difference as seconds, use total_seconds() function.

```print(tdelta.total_seconds())
262814.954```

Datetime module does not provide functions and attributes to get time difference in minutes & hours. For that, you will need to divide the above time difference in seconds, by 60 for minutes and 3600 for hours.

```# time difference in minutes
>>> tdelta.seconds/60
60
>>> tdelta.total_seconds()/60
4380.249233333334

#time difference in hours
>>> tdelta.seconds/3600
1
>>> tdelta.total_seconds()/3600
73.0041538888889```

In this article, we have learnt how to easily create date, time, datetime objects in python and use them calculate time difference in days, hours, minutes & seconds. You can also use them to calculate date/time difference between results of database queries. Most python libraries for database connection return a datetime database column as datetime object. So you can use the above approach to calculate datetime difference between date strings, datetime-based database columns, or simply datetime information available as numbers. Basically, you need to convert them into datetime object first and then calculate their difference.