Python: Convert timedelta to milliseconds

This article will discuss how we can convert the datetime module’s timedelta object to total milliseconds in python.

Background:

In Python’s datetime module, the datetime.timedelta represents the duration between two dates, time, or datetime objects. For example, we have two timestamps as datetime objects,

from datetime import datetime

timestamp_1   =   datetime(2019, 6, 3, 8, 12, 16, 100000)
timestamp_2   =   datetime(2019, 6, 3, 9, 17, 11, 200002)

If you want the difference between these two timestamps, you can subtract these two datetime objects. It will give a timedelta object i.e.

# Get different between two datetime as timedelta object.
diff = (timestamp_2 - timestamp_1)

print(diff)
print( type(diff) )

Output:

1:04:55.100002
<class 'datetime.timedelta'>

This timedelta object contains the time difference between two datetime objects. But if you print it, then you will get the absolute difference between two datetimes in hh:mm::ss.ffffff format. What if, you want the complete duration in total milliseconds only? For that we can convert this timedelta object to milliseconds only. Let’s see how to do that,

Convert timedelta to milliseconds in python

The timedelta object represents a duration between to timestamps. It has a function total_seconds(), which gives the total seconds in the duration pointed by timedelta object. We can multiply it by 1000, to get the complete duration in milliseconds. For example,

# Convert timedelta object to Milliseconds
diff_in_milliseconds = diff.total_seconds() * 1000

print("Total Time Difference : {} Milliseconds".format(diff_in_milliseconds) )

Output:

Total Time Difference : 3895100.0020000003 Milliseconds

We got the difference between two timestamps in milliseconds only by converting timedelta to milliseconds.

But we got the milliseconds with decimal part too. If you interested in only approx. absolute number, then you can round off the value i.e.

# Convert timedelta object to Milliseconds
diff_in_milliseconds = diff.total_seconds() * 1000

# Round of the Milliseconds value
diff_in_milliseconds = round(diff_in_milliseconds)

print("Total Time Difference : {} Milliseconds".format(diff_in_milliseconds) )

Output:

Total Time Difference : 3895100 Milliseconds

The complete working example is as follows,

from datetime import datetime

timestamp_1   =   datetime(2019, 6, 3, 8, 12, 16, 100000)
timestamp_2   =   datetime(2019, 6, 3, 9, 17, 11, 200002)

# Get different between two datetime as timedelta object.
diff = (timestamp_2 - timestamp_1)

print(diff)
print( type(diff) )

# Convert timedelta object to Milliseconds
diff_in_milliseconds = diff.total_seconds() * 1000

print("Total Time Difference : {} Milliseconds".format(diff_in_milliseconds) )

# Round of the Milliseconds value
diff_in_milliseconds = round(diff_in_milliseconds)

print("Total Time Difference : {} Milliseconds".format(diff_in_milliseconds) )

Output:

1:04:55.100002
<class 'datetime.timedelta'>
Total Time Difference : 3895100.0020000003 Milliseconds
Total Time Difference : 3895100 Milliseconds

Summary:

Today we learned that how we can convert datetime.timedelta to milliseconds in python.

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