Convert timedelta to Milliseconds & Microseconds in Python (2 Examples)
In this article, you’ll learn how to convert a timedelta object to milliseconds and microseconds in Python.
The article contains these topics:
If you would like to learn more about these topics, keep reading!
Exemplifying Data & Loaded Modules
Firstly, we should load the datetime module.
import datetime # Loading datetime module
Also, we should define a sample timedelta object as shown below:
td = datetime.timedelta(days=3, seconds=50, microseconds=479021) # timedelta construction print(td) # printing timedelta # 3 days, 0:00:50.479021
Example 1: Express in Milliseconds & Microseconds via timedelta Attributes: days, seconds, microseconds
To split the timedelta into milliseconds and microseconds, we can use the microseconds attribute and some simple math operations. Then we can save the results as integers.
milliseconds = td.microseconds // 1000 # extracting milliseconds field print(milliseconds) # printing milliseconds field # 479 microseconds = td.microseconds % 1000 # extracting microseconds field print(microseconds) # printing microseconds field # 21
Now we can add the other time fields of the timedelta object to the milliseconds/microseconds that we previously extracted. For the implementation, we make use of the timedelta attributes and simple math operations like in the previous Python code. The example for the units of milliseconds is shown below.
milliseconds += td.days * 24 * 60 * 60 * 1000 + td.seconds * 1000 # converting the rest of fields into milliseconds print(milliseconds) # printing milliseconds # 259250479
Example 2: Express in Microseconds and Milliseconds via timedelta Method: total_seconds()
In Example 2, I’ll illustrate how to use the total_seconds() method to convert a timedelta object to micro/milli-seconds. In the first step, we express the timedelta in seconds via total_seconds() as shown below.
td_in_sec = td.total_seconds() # converting timedelta in seconds print(td_in_sec) # printing timedelta in seconds # 259250.479021
Afterwards, we multiply it by a million to convert it to microseconds.
td_in_micro = int(td_in_sec * 1000000) print(td_in_micro) # printing in microseconds # 259250479021
Finally, the millisecond’s part can be extracted as follows.
td_milli_part = td_in_micro// 1000 # extracting milliseconds field print(td_milli_part) # printing millisecond field # 259250479
Video & Further Resources
Do you want to learn more about the conversion of timedelta to milliseconds & microseconds? Then you could watch the following video on my YouTube channel. In the video, I illustrate the examples of the present article.
The YouTube video will be added soon.
Furthermore, you could have a look at some other articles on my website:
- Convert String to timedelta in Python
- Convert datetime into String with Milliseconds in Python
- Convert Milliseconds into datetime in Python
- Convert timedelta to Seconds in Python in R
- Introduction to Python Programming
You have learned in this tutorial how to express timedelta objects in milliseconds & microseconds in Python programming. If you have additional questions, please let me know in the comments section below.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get further information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.