Convert datetime to String without Microsecond Component in Python (3 Examples)

 

On this site, you’ll learn how to convert a datetime object to a string without a microsecond component in the Python programming language.

The following sections are explained in this article:

Let’s do this!

 

Importing datetime module & Creating Example Data

First, we have to import the datetime module:

from datetime import datetime

Next, we can create an example datetime object using the now() function as shown below:

my_datetime = datetime.now()
print(my_datetime)
# 2021-11-19 07:22:34.542583

Our example date is the 19th of November 2021 at 07:22:34am. As you can see, our example datetime object also contains a milliseconds and microseconds component showing the value 542583.

Let’s convert our datetime object to a character string without these microseconds!

 

Example 1: Remove Microseconds from datetime Object Using isoformat() Function

This example uses the isoformat() function of the datetime module to delete the milliseconds and microseconds from our datetime object.

This method takes a datetime object and returns a character string representing the date in ISO 8601 format.

To get rid of the microseconds component, we have to specify the keyword ‘seconds’ within the isoformat function.

my_string1 = my_datetime.isoformat(' ', 'seconds')
print(my_string1)
# 2021-11-19 07:22:34

 

Example 2: Remove Microseconds from datetime Object Using replace() Function

This example uses the replace() function to create a string object without microseconds:

my_string2 = my_datetime.replace(microsecond = 0)
print(my_string2)
# 2021-11-19 07:22:34

 

Example 3: Remove Microseconds from datetime Object Using str() & slice() Functions

Another approach to exclude microseconds from a datetime object is based on the str() and slice() functions.

The str() function converts a datetime object into a string with microseconds.

The slice function can be applied to this string to return only the first 19 characters. This deletes the last part of the string, i.e. the microseconds component.

my_string3 = str(my_datetime)[:19]
print(my_string3)
# 2021-11-19 07:22:34

 

Video, Further Resources & Summary

If you need further explanations on how to set a datetime object to a string without the microsecond component in Python, you may have a look at the following video from the YouTube channel Pretty printed.

The video is about parsing and formatting dates using the strftime() and strptime() functions in Python.

 

 

Furthermore, you may have a look at the other Python and datetime tutorials on this website:

Summary: This post has illustrated how to change a datetime object to a string format without microseconds in the Python programming language. In case you have any additional questions, you may leave a comment below.

This article 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. Required fields are marked *

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

Top