Python: Get difference between two datetimes in milliseconds

In this article, we will discuss how to get the difference between two timestamps in milliseconds in python.

Python provides a datetime module for manipulation of date and time. It consists of following classes,

  • datetime.date: An object of date class specifies a date using year, month and day
  • datetime.time: An object of time class specifies a timestamp using hour, minute, second, microsecond, and tzinfo.
  • datetime.datetime: An object of datetime is a combination of a date and a time.
  • datetime.timedelta: A duration, that represents the difference between two dates or times or datetimes.
  • datetime.tzinfo: It contains the timezone information.

Now to get the difference between two timestamps in python, we will use the datetime module. First we will create start and end datetime objects from the string timestamps. Then we will subtract the datetime objects to get the duration in the datetime.timedelta object. Then using the properties of timedelta object, we will fetch the milliseconds in between the two timestamps. Let’s understand with some examples,

Python Example 1: Get difference between two timestamps in milliseconds

Convert the timestamps in string format to datetime objects. Then subtract them and get the timedelta object. Then use the total_seconds() function of timedelta to get the complete duration between two timestamps in seconds and then convert it to milliseconds. For example,

from datetime import datetime

date_1 = '24/7/2021 11:13:08.230010'
date_2 = '24/7/2021 11:14:18.333338'

date_format_str = '%d/%m/%Y %H:%M:%S.%f'

start = datetime.strptime(date_1, date_format_str)
end =   datetime.strptime(date_2, date_format_str)

# Get the interval between two datetimes as timedelta object
diff = end - start

# Get the interval in milliseconds
diff_in_milli_secs = diff.total_seconds() * 1000

print('Difference between two datetimes in milli seconds:')
print(diff_in_milli_secs)

Output:

Difference between two datetimes in milli seconds:
70103.32800000001

How did it work ?

We createed the two datetime objects from the two timestamps in the string format, by passing the date and it’s format in the strptime() function. Then we subtracted these two datetime objects and got the datetime.timedelta object, which represents the duration between two dates. The timedelta class has a member function total_seconds(), which represents the complete duration in seconds (including the fractional part till milliseconds). We used that to fetch the difference bewteen two timestamp in seconds and then we converted it to milliseconds by multiplying by 1000.

Python Example 2: Get difference between two timestamps in milliseconds

Let’s checkout an another example, where both the timestamps are more than 1 day apart,

from datetime import datetime

date_1 = '24/7/2021 11:13:08.230010'
date_2 = '25/7/2021 11:14:18.333338'

date_format_str = '%d/%m/%Y %H:%M:%S.%f'

start = datetime.strptime(date_1, date_format_str)
end =   datetime.strptime(date_2, date_format_str)

# Get the interval between two datetimes as timedelta object
diff = end - start

# Get the interval in milliseconds
diff_in_milli_secs = diff.total_seconds() * 1000

print('Difference between two datetimes in milli seconds:')
print(diff_in_milli_secs)

Output:

Difference between two datetimes in milli seconds:
86470103.328

Python Example 3: Get difference between two timestamps in milliseconds

Let’s checkout an another example, where both the timestamps are more than 2 years apart,

from datetime import datetime

date_1 = '2/7/2021 21:55:12.230010'
date_2 = '24/9/2023 11:13:08.333338'

date_format_str = '%d/%m/%Y %H:%M:%S.%f'

start = datetime.strptime(date_1, date_format_str)
end =   datetime.strptime(date_2, date_format_str)

# Get the interval between two datetimes as timedelta object
diff = end - start

# Get the interval in milliseconds
diff_in_milli_secs = diff.total_seconds() * 1000

print('Difference between two datetimes in milli seconds:')
print(diff_in_milli_secs)

Output:

Difference between two datetimes in milli seconds:
70291076103.328

Python Example 4: Get difference between two datetimes in milliseconds

If we have complete datetime objects instead of only timestamp strings, in that case lets see how to calculate the difference between two datetimes in milliseconds,

from datetime import datetime

date_1 = datetime(2021, 7, 2, 21, 55, 12)
date_2 = datetime(2021, 7, 24, 11, 13, 8)

# Get the interval between two datetimes as timedelta object
diff = date_2 - date_1

# Get the interval in milliseconds
diff_in_milli_secs = diff.total_seconds() * 1000

print('Difference between two datetimes in milli seconds:')
print(diff_in_milli_secs)

Output:

Difference between two datetimes in milli seconds:
1862276000.0

Python Example 5: Get difference between two datetimes in milliseconds using pandas

Suppose we have two timestamps in string format. We can convert them to datetime object using pandas.to_datetime() function. Then we will subtract the datetime objects to get the duration in the datetime.timedelta object. Then using the properties of timedelta object, we will fetch the total seconds in between the two timestamps (including the fractional part till milliseconds) and then multiply that by 1000 to get the complete duration in milliseconds. For example,

import pandas as pd
from datetime import datetime

date_1 = '2/7/2021 21:55:12.230010'
date_2 = '24/9/2023 11:13:08.333338'

date_format_str = '%d/%m/%Y %H:%M:%S.%f'

start = pd.to_datetime(date_1, format=date_format_str)
end = pd.to_datetime(date_2, format=date_format_str)

# Get the interval between two datetimes as timedelta object
diff = end - start

# Get the interval in milliseconds
diff_in_milli_secs = diff.total_seconds() * 1000

print('Difference between two datetimes in milli seconds:')
print(diff_in_milli_secs)

Output:

Difference between two datetimes in milli seconds:
70291076103.328

Summary:

We learned that, in python how to get the difference between two datetimes in milliseconds.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top