# Extract Time Difference Between Two datetimes in Milliseconds in Python (2 Examples)

In this article, you’ll learn how to print the difference between two datetimes in milliseconds in Python.

## Introducing Example Data

We have to load the datetime module to Python as shown below:

 import datetime # Import datetime

The following datetime objects x1 and x2 will be used for this tutorial.

 x1 = datetime.datetime(2021, 3, 19, 9, 10, 00) # Example date print(x1) # 2021-03-19 09:10:00
 x2 = datetime.datetime(2020, 12, 30, 18, 45, 00) # Example date print(x2) # 2020-12-30 18:45:00

## Example 1: Get Time Difference of Two datetimes

If we want the difference between our datetimes, we can subtract one from another by using the – operator:

 diff = x1 - x2 # Calculate difference print(diff) # 78 days, 14:25:00

There are 78 days, 14 hours, and 25 minutes between our two datetime objects.

## Example 2: Return Difference in Milliseconds

To transform the above information into milliseconds, we use the total_seconds function and multiply the result with one thousand:

 milli_secs_diff = diff.total_seconds() * 1000 # Time in milliseconds print(milli_secs_diff) # 6791100000.0

Now, we have the result in milliseconds as wanted: 6791100000.0 milliseconds.

Note: This article was created in collaboration with Matthias Bäuerlen. Matthias is a programmer who helps to create tutorials on the Python programming language. You might find more info about Matthias and his other articles on his profile page.