time.strptime() method

time.strptime() method converts the string representing time to the struct_time object.

Example: Converting string to struct_time object.

This code uses the time module to parse a formatted string representing a date and time and convert it into a time.struct_time object. The time.strptime() function is used for this purpose.

Python3




import time
string = "Tue, 03 Aug 2021 10:45:08"
obj = time.strptime(string, "%a, %d %b %Y %H:%M:%S")
print(obj)


Output

time.struct_time(tm_year=2021, tm_mon=8, tm_mday=3, tm_hour=10, tm_min=45, tm_sec=8, tm_wday=1, tm_yday=215, tm_isdst=-1)



Python Time Module

In this article, we will discuss the time module and various functions provided by this module with the help of good examples. 

As the name suggests Python time module allows to work with time in Python. It allows functionality like getting the current time, pausing the Program from executing, etc. So before starting with this module, we need to import it. 

Similar Reads

Importing time module

The time module comes with Python’s standard utility module, so there is no need to install it externally. We can simply import it using the import statement....

What is epoch?

The epoch is the point where the time starts and is platform-dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC), and leap seconds are not counted towards the time in seconds since the epoch. To check what the epoch is on a given platform we can use time.gmtime(0)....

Getting time string from seconds

...

Delaying Execution of Programs

...

time.struct_time Class

time.ctime() function returns a 24 character time string but takes seconds as argument and computes time till mentioned seconds. If no argument is passed, time is calculated till the present....

time.localtime() method

...

time.mktime() method

Execution can be delayed using time.sleep() method. This method is used to halt the program execution for the time specified in the arguments....

time.gmtime() method

...

time.strftime() method

Struct_time class helps to access local time i.e. non-epochal timestamps. It returns a named tuple whose value can be accessed by both index and attribute name. Its object contains the following attributes –...

time.asctime() method

localtime() method returns the struct_time object in local time. It takes the number of seconds passed since epoch as an argument. If the seconds parameter is not given then the current time returned by time.time() method is used....

time.strptime() method

...