Extract Only Microseconds & Milliseconds from datetime Object in Python (Example)
In this tutorial, you’ll learn how to extract only microseconds and milliseconds using the Python programming language.
The table of content is structured as follows:
Let’s start with the interview.
Imported Libraries & Sample Construction
Initially, we need to import datetime from the datetime module. Afterwards, we will construct some sample data for examples 1 and 3.
from datetime import datetime import time # Sample for Example 1 dt1 = datetime(2001, 8, 15, 23, 14, 4, 456123) print(dt1) # 2001-08-15 23:14:04.456123 # Sample for Example 3 dt_list = [datetime(2001, 8, 15, 23, 14, 4, 125001), datetime(2001, 8, 15, 23, 14, 4, 175175), datetime(2001, 8, 15, 23, 14, 4, 300127), datetime(2001, 8, 15, 23, 14, 4, 750750)] print(dt_list) #[datetime.datetime(2001, 8, 15, 23, 14, 4, 125001), # datetime.datetime(2001, 8, 15, 23, 14, 4, 175175), # datetime.datetime(2001, 8, 15, 23, 14, 4, 300127), # datetime.datetime(2001, 8, 15, 23, 14, 4, 750750)]
Example 1: Using the Fields of the datetime Object
Datetime objects have a microsecond field which can be utilized to get the millisecond and microsecond part together. Afterwards, by dividing this number by 1000 we get the milliseconds as the quotient and microseconds part as the remainder.
combined_time = dt1.microsecond millisec = combined_time // 1000 microsec = combined_time % 1000 print(millisec, microsec) # 456 123
Example 2: Subtracting the datetime Object from Itself Except the Microsecond Part
Another method we can use is subtracting a datetime copy which has its microsecond part set to 0.
dt2 = datetime(dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute, dt1.second) diff = dt1 - dt2 combined_time = diff.total_seconds() * 1000000 millisec = combined_time // 1000 microsec = combined_time % 1000 print(millisec, microsec) # 456.0 123.0
Example 3: Grouping datetime Objects Based on Their Milliseconds
Let’s say we have four datetime objects as in the following code, and we have a problem of checking if the millisecond parts of the datetimes are equal with their microseconds part.
equal_list = [] for x in range(len(dt_list)): # The millisecond and microsecond parts are extracted combined_time = dt_list[x].microsecond millisec = combined_time // 1000 microsec = combined_time % 1000 # Their equality is checked and set to a boolean variable, then added to a list. equal_check = millisec == microsec equal_list.append(equal_check) print(equal_list) # [False, True, False, True]
The output states that the 2nd and 4th datetimes had the same quantities for their millisecond and microsecond parts.
Video, Further Resources & Summary
Do you need more explanations on how to extract only microseconds and milliseconds? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
The YouTube video will be added soon.
Furthermore, you could have a look at some other tutorials on Statistics Globe:
- Dates & Times in Python – datetime Module
- Introduction to timedelta Objects in Python
- Get First & Last Day of Month in Python
- Count Working Days Between Two Dates Excluding Weekends in Python
- Calculate Time Difference Between Two Columns of pandas DataFrame in Python
- Calculate Number of Hours, Minutes & Seconds Between Two datetimes in Python
- Calculate Number of Years, Months & Days Between Two Dates in Python
- Calculate Time Difference Between Two datetime Objects in Python
- Calculate Time Difference in Milliseconds Between Two datetimes
- How to Add & Subtract Weeks to & from Date in Python
- Add Days, Months & Years to datetime Object
- Introduction to Python
This post has shown how to extract only microseconds and milliseconds. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.