How to use strptime with milliseconds in Python


Working with dates and times is a common task in programming, and Python provides several modules to handle such operations. The datetime module is one of the most commonly used modules for manipulating dates and times. It offers a variety of functions and classes, including the strptime() method, which allows us to convert a string representation of a date and time into a datetime object. In this article, we will understand how we can use strptime with milliseconds in Python with the help of detailed examples.

Understanding strptime()

The strptime() method in the datetime module is used to parse a string representation of a date and time into a datetime object. It takes two arguments: the first argument is the string to be parsed, and the second argument is a format string that specifies the expected format of the input string. The format string consists of format codes that represent different parts of the date and time.

By default, the strptime() method does not support parsing milliseconds. The available format codes for milliseconds are limited, and they do not correspond to the usual %f format code used for microseconds. However, we can overcome this limitation by using a combination of string slicing and integer conversion.

Using strptime with Milliseconds:

To use strptime() with milliseconds, we need to follow a two-step process:

  • Parse the string without milliseconds using strptime().

  • Extract the milliseconds separately and add them to the parsed datetime object.

Algorithm

  • Import the datetime module.

  • Define the input string representing the date and time with milliseconds.

  • Define the format string without milliseconds.

  • Parse the string without milliseconds using strptime() by excluding the last four characters (milliseconds) from the input string.

  • Extract the milliseconds separately from the input string.

  • Replace the microseconds in the parsed datetime object with the extracted milliseconds by multiplying them by 1000.

  • Print or use the parsed datetime object with milliseconds.

Example 1: Parsing a Date and Time String with Milliseconds

Suppose we have a date and time string in the format '2023-05-29 12:34:56.789', and we want to parse it into a datetime object with milliseconds. We will achieve this in the below example.

In the below example, we first parse the string without milliseconds using strptime() by excluding the last four characters (milliseconds) from the input string. Then, we extract the milliseconds separately from the input string and multiply them by 1000 to convert them into microseconds. Finally, we replace the microseconds in the parsed datetime object with the extracted milliseconds.

from datetime import datetime

input_string = '2023-05-29 12:34:56.789'
format_string = '%Y-%m-%d %H:%M:%S'

# Step 1: Parse the string without milliseconds
parsed_datetime = datetime.strptime(input_string[:-4], format_string)

# Step 2: Extract milliseconds separately and add them to the parsed datetime object
milliseconds = int(input_string[-3:])
parsed_datetime = parsed_datetime.replace(microsecond=milliseconds * 1000)

print(parsed_datetime)

Output

2023-05-29 12:34:56.789000

Example 2: Parsing a Time String with Milliseconds

Let's consider a scenario where we have a time string in the format '12:34:56.789', and we want to parse it into a datetime object with milliseconds. We will achieve this in the below example.

In the below example, we follow the same two-step process as before. We parse the string without milliseconds using strptime() by excluding the last four characters (milliseconds) from the input string. Then, we extract the milliseconds separately and add them to the parsed datetime object.

from datetime import datetime

input_string = '12:34:56.789'
format_string = '%H:%M:%S'

# Step 1: Parse the string without milliseconds
parsed_datetime = datetime.strptime(input_string[:-4], format_string)

# Step 2: Extract milliseconds separately and add them to the parsed datetime object
milliseconds = int(input_string[-3:])
parsed_datetime = parsed_datetime.replace(microsecond=milliseconds * 1000)

print(parsed_datetime)

Output

1900-01-01 12:34:56.789000

Conclusion

In this article, we discussed how we can use strptime with milliseconds in Python. Although strptime does not support milliseconds inherently, we can overcome this limitation by using string slicing and integer conversion. By following a two-step process of parsing the string without milliseconds and then extracting and adding the milliseconds separately, we can achieve accurate parsing of date and time strings with milliseconds.

Updated on: 13-Oct-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements