Python time strptime() function Last Updated : 13 Sep, 2021 Comments Improve Suggest changes Like Article Like Report 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. Syntax: time.strptime(date_time_srting,directive) Here, 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 Python3 import time formatted_date = time.strptime(" 02 Dec 1996", " %d %b %Y") print(formatted_date) Output: 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 Python3 import time print(time.strptime("02/12/1996 5:53","%m/%d/%Y %H:%M")) Output: 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. Python3 import time as datetime datetime_str = '08/1/18 3:55:6' try: datetime_object = datetime.strptime(datetime_str, '%m/%d/%y') except ValueError as e: print('ValueError Raised:', e) time_str = '25::55::26' try: time_object = time.strptime(time_str, '%H::%M::%S') except ValueError as e: print('ValueError:', e) Output: ValueError Raised: unconverted data remains: 3:55:6 ValueError: time data '25::55::26' does not match format '%H::%M::%S' Comment More infoAdvertise with us Next Article Python time strptime() function D ddeevviissaavviittaa Follow Improve Article Tags : Python Blogathon Blogathon-2021 Python time-module Practice Tags : python Similar Reads Python - time.ctime() Method Python time.ctime() method converts a time in seconds since the epoch to a string in local time. This is equivalent to asctime(localtime(seconds)). Current time is returned by localtime() is used when the time tuple is not present. Syntax: time.ctime([ sec ]) Parameter: sec: number of seconds to be 2 min read Python | time.get_clock_info() method Time module in Python provides various time related functions. time.get_clock_info() method in Time module is used to get the information on the specified clock name. This method return the information as a namespace object. The name of supported clocks and the method used to read that clock value a 2 min read Python | time.gmtime() method Time module in Python provides various time-related functions. This module comes under Pythonâs standard utility modules. time.gmtime() method of Time module is used to convert a time expressed in seconds since the epoch to a time.struct_time object in UTC in which tm_isdst attribute is always 0. To 3 min read Python | time.localtime() method Time module in Python provides handy tools to work with time-related tasks. One of its most useful functions is time.localtime(), which converts time expressed in seconds since the epoch (January 1, 1970) into a local time representation. It returns a time.struct_time object, which is a tuple-like s 2 min read Python | time.mktime() method time.mktime() method of Time module is used to convert a time.struct_time object or a tuple containing 9 elements corresponding to time.struct_time object to time in seconds passed since epoch in local time. This method is the inverse function of time.localtime() which converts the time expressed in 4 min read Python | time.monotonic() method Python time.monotonic() method is used to get the value of a monotonic clock. A monotonic clock is a clock that can not go backward. As the reference point of the returned value of the monotonic clock is undefined, only the difference between the results of consecutive calls is valid. Python time.mo 2 min read Python | time.monotonic_ns() method Time module in Python provides various time-related functions. This module comes under Pythonâs standard utility modules. time.monotonic_ns() method of time module in Python is used to get the value of a monotonic clock in nanoseconds. This method is similar to time.monotonic() method which returns 3 min read time.perf_counter() function in Python The time module provides various time-related functions. We must import the time module before using perf_counter() so we can access the function without throwing any errors.The perf_counter() function always returns the float value of time in seconds. Return the value (in fractional seconds) of a p 4 min read Python time.perf_counter_ns() Function Python time.perf_counter_ns() function gives the integer value of time in nanoseconds. Syntax: from time import perf_counter_ns We can find the elapsed time by using start and stop functions. We can also find the elapsed time during the whole program by subtracting stoptime and starttime Example 1: 1 min read time.process_time() function in Python time.process_time() is a function in Pythonâs time module that returns the CPU time used by the current process as a floating-point number in seconds. Unlike time.time(), which measures real (wall-clock) time, time.process_time() focuses on the time spent by the CPU executing the process, excluding 4 min read Like