Skip to content
Related Articles

Related Articles

Python time strptime() function

Improve Article
Save Article
  • Last Updated : 13 Sep, 2021
Improve Article
Save Article

The strptime() function in Python is used to format and return a string representation of date and time. It takes in the date, time, or both as an input, and parses it according to the directives given to it. It raises ValueError if the string cannot be formatted according to the provided directives.




  • date_time_string: It is a necessary string parameter where the date to be formatted is provided.
  • directive: This parameter represents the condition for parsing the given string. It is also a necessary parameter.

Directives Table:

The following tables list the supported directives in python.

format codemeaningexample
%aAbbreviated weekday nameSun, Mon
%AFull weekday name Sunday, Monday
%wWeekday as decimal number0…6
%dDay of the month as a zero-padded decimal01, 02
%b Abbreviated month nameJan, Feb
%mmonth as a zero padded decimal number01, 02
%B Full month nameJanuary, February
%yyear without century as a zero padded decimal number99, 00 
%Yyear with century as a decimal number2000, 1999
%Hhour(24 hour clock) as a zero padded decimal number01, 23
%Ihour(12 hour clock) as a zero padded decimal number01, 12
%plocale’s AM or PMAM, PM
%MMinute as a zero padded decimal number01, 59
%SSecond as a zero padded decimal number01, 59
%fmicrosecond as a decimal number, zero padded on the left side000000, 999999
%zUTC offset in the form +HHMM or -HHMM 
%ZTime zone name 
%jday of the year as a zero padded decimal number001, 365
%UWeek number of the year (Sunday being the first)0, 6
%WWeek number of the year00, 53
%clocale’s appropriate date and time representationMon Sep 30 07:06:05 2013
%xlocale’s appropriate date representation11/30/98
%Xlocale’s appropriate time representation10:03:43
%%A literal ‘%’ character%

Examples 1: Python string to date


import time
formatted_date = time.strptime(" 02 Dec 1996",
                               " %d %b %Y")


time.struct_time(tm_year=1996, tm_mon=12, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=337, tm_isdst=-1)

Example 2: Python string to date and time


import time
print(time.strptime("02/12/1996 5:53","%m/%d/%Y %H:%M"))


time.struct_time(tm_year=1996, tm_mon=2, tm_mday=12, tm_hour=5, tm_min=53, tm_sec=0, tm_wday=0, tm_yday=43, tm_isdst=-1)

Example 3: Value error

In this example, if time directives will not match then occurred error.


import time as datetime
datetime_str = '08/1/18 3:55:6'
    datetime_object = datetime.strptime(datetime_str, '%m/%d/%y')
except ValueError as e:
    print('ValueError Raised:', e)
time_str = '25::55::26'
    time_object = time.strptime(time_str, '%H::%M::%S')
except ValueError as e:
    print('ValueError:', e)


ValueError Raised: unconverted data remains:  3:55:6
ValueError: time data '25::55::26' does not match format '%H::%M::%S'

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!