Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to use strptime with milliseconds in Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

strptime() function in python converts the string into DateTime objects. The strptime() is a class method that takes two arguments : 

  • string that should be converted to datetime object
  • format string used to parse the string.

These two string arguments are mandatory for converting a string into DateTime object.

Syntax:

strptime(date_string, format_string)

List of Format codes:

Format stringInterpretationExample
%a Weekday 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
%f Microsecond 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
%x preferred date representation.

08/16/88

08/16/1998

%Xpreferred time representation.

21:30:00

%% – A literal ‘%’ character.

To use this function to produce milliseconds in python %f is used in format code.

Given below are some implementations.

Example 1: Time with milliseconds

Python3




from datetime import datetime
  
datetime_string = "15/06/2021 13:30:15.120"
  
print(type(datetime_string))
  
format = "%d/%m/%Y %H:%M:%S.%f"
  
# converting datetime string to datetime 
# object with milliseconds..
date_object = datetime.strptime(datetime_string, format)
  
print("date_object =", date_object)
  
# Type is datetime object
print(type(date_object))

Output:

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

Example 2: time with milliseconds

Python3




from datetime import datetime
  
# Getting current datetime and converting
# it to string
date_str = str(datetime.now())
  
print(type(date_str))
  
print(datetime.strptime(date_str, '%Y-%m-%d %H:%M:%S.%f'))

Output:

<class 'str'>
2021-08-01 15:27:59.979673

Example 3: time with milliseconds

Python3




from datetime import datetime
  
# Using strptime() with milliseconds
  
date_time = datetime.strptime(
    "17 Oct 2021 15:48:35.525001", "%d %b %Y %H:%M:%S.%f")
  
print(date_time)

Output:

2021-10-17 15:48:35.525001

My Personal Notes arrow_drop_up
Last Updated : 23 Aug, 2021
Like Article
Save Article
Similar Reads
Related Tutorials