Open In App

How to use strptime with milliseconds in Python

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The strptime() method from the datetime module in Python helps convert string representations into datetime objects. To include milliseconds while parsing time, the %f format code is used. For example, let's consider we have a timestamp string like this: "15/06/2021 13:30:15.120", the string returns a specific date and time including milliseconds (.120). To work with it in Python, such as comparing times, calculating durations, or formatting, we need to convert it into a datetime object. Here's how to do it:

Python
from datetime import datetime

s = "15/06/2021 13:30:15.120"
format = "%d/%m/%Y %H:%M:%S.%f"

dt_obj = datetime.strptime(s, format)
print(dt_obj)

Output
2021-06-15 13:30:15.120000

Syntax

datetime.strptime(date_string, format_string)

Parameters:

  • date_string: The string containing the date and/or time to be parsed.
  • format_string: The format that tells Python how to interpret the date_string.

Format Codes for Reference

Here's a short list of useful format codes you might need:

Format stringMeaningExample
%aWeekday as an abbreviated name.Sun, Mon, …, Sat 
%AWeekday as full name.Sunday, Monday, …, Saturday 
%wWeekday as a decimal number, 0 is Sunday and 6 is Saturday.0, 1, …, 6
%dDay of the month as a zero-padded decimal number.01, 02, …, 31
%bMonth as an abbreviated name.Jan, Feb, …, Dec
%BMonth.January, February, …, December
%mMonth01, 02, …, 12
%yYear without century.00, 01, …, 99
%YYear with century.0001, 0002, …, 2013, 2014, …, 9998, 9999
%HHour (24-hour clock).00, 01, …, 23
%IHour (12-hour clock).01, 02, …, 12
%peither AM or PM.AM, PM
%MMinute.00, 01, …, 59
%SSecond.00, 01, …, 59
%fMicrosecond as a decimal number.000000, 000001, …, 999999
%zUTC offset in the form ±HHMM[SS[.ffffff]] .+0000, -0400, +1030, +063415, -030712.345216
%ZTime zone (UTC, GMT) 
%jDay of the year.001, 002, …, 366
%UWeek number of the year (Sunday as the first day of the week). 00, 01, …, 53
%WWeek number of the year (Monday as the first day of the week) as a decimal number. 00, 01, …, 53
%cPreferred date and time representation.Tue Aug 16 21:30:00 1998
%xPreferred date representation.

08/16/88

08/16/1998

%XPreferred time representation.

21:30:00

%% - A literal '%' character.

Use %f when you want to include milliseconds or microseconds in your datetime string.

Examples of strptime Method

Example 1: Parsing a Time String with Milliseconds

In this example, we convert a string with milliseconds into a datetime object.

Python
from datetime import datetime

s = "15/06/2021 13:30:15.120"
format = "%d/%m/%Y %H:%M:%S.%f"

obj = datetime.strptime(s, format)

print("date_object =", obj)
print("Type:", type(obj))

Output
date_object = 2021-06-15 13:30:15.120000
Type: <class 'datetime.datetime'>

Explanation: The %f in the format string ensures that .120 gets interpreted as 120000 microseconds (or 0.120 seconds).

Example 2: Convert Current Datetime to String and Back

This shows how to convert the current datetime to a string and then parse it back with milliseconds.

Python
from datetime import datetime

s = str(datetime.now())

print(type(s))

print(datetime.strptime(s, '%Y-%m-%d %H:%M:%S.%f'))

Output
<class 'str'>
2025-04-07 09:43:09.006822

Explanation: We convert the current datetime to a string using str(), then use strptime() with %f to parse the microsecond component.

Example 3: Parsing a Date String with Abbreviated Month and Microseconds

This example shows a string with an abbreviated month name and a full microsecond value.

Python
from datetime import datetime

date = datetime.strptime("17 Oct 2021 15:48:35.525001", "%d %b %Y %H:%M:%S.%f")
print(date)

Output
2021-10-17 15:48:35.525001

Explanation: Here, %b is used to parse the abbreviated month name (Oct), and %f ensures microseconds (525001) are included in the result.


Article Tags :
Practice Tags :

Similar Reads