Convert datetime into String with Milliseconds in Python (3 Examples)

 

On this site, you’ll learn how to convert a datetime object into string with milliseconds in the Python programming language.

This article contains the sections below:

Let’s dive into it!

 

Importing datetime Module & Creating Example Data

In this example we are going to use the datetime module. For this, we first need to import the module to Python:

from datetime import datetime

Next, we can create a datetime object as shown below:

my_datetime = datetime.today()
print(my_datetime)
# 2021-11-25 13:36:44.396090

As you can see, the previous Python syntax has created a datetime object with milliseconds.

The following examples explain how to transform our datetime object to a string object that still contains these milliseconds.

 

Example 1: Transform datetime Object to String with Milliseconds Using isoformat() Function

This example uses isoformat() function from the datetime module to remove milliseconds from our output.

The isoformat function takes a datetime object as input and returns a string representing the date in ISO 8601 format.

my_string1 = my_datetime.isoformat(sep = ' ', timespec = 'milliseconds')
print(my_string1)
# 2021-11-25 13:36:44.396

As you can see, we have created a new data object called my_string1 that contains our example date with milliseconds formatted as a string.

 

Example 2: Transform datetime Object to String with Milliseconds Using strftime() Function

In this example, we’ll use the strftime() function to convert a datetime object to a character string with milliseconds and microseconds.

More precisely, we’ll use the format ‘%Y-%m-%d %H:%M:%S.%f’ (i.e. years, months, days, hours, minutes, seconds, and milliseconds).

my_string2 = my_datetime.strftime('%Y-%m-%d %H:%M:%S.%f')
print(my_string2)
# 2021-11-25 13:36:44.396090

 

Example 3: Transform datetime Object to String with Milliseconds Using str() Function

Alternatively to the previous examples, we can also use the str() function to convert a datetime object with milliseconds to a string.

Consider the following Python code and its output:

my_string3 = str(my_datetime)
print(my_string3)
# 2021-11-25 13:36:44.396090

 

Video, Further Resources & Summary

Do you need more explanations on how to change a date and time into a string with milliseconds in Python? Then have a look at the following video of the PyLenin YouTube channel.

In the video, the speaker explains how to use strptime and strftime for converting strings to datetime objects and vice-versa.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you may have a look at some other tutorials on the Statistics Globe website:

Summary: This tutorial demonstrated how to transform a datetime object into a string with a milliseconds component in the Python programming language. If you have any additional questions on how to keep milliseconds in character strings, please leave them in the comments.

This post was created in collaboration with Gottumukkala Sravan Kumar. You may find more information about Gottumukkala Sravan Kumar and his other articles on his profile page.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top