The time.strptime()function converts a string representing time/date to a struct_time object. struct_time is a data structure defined in the time module, used to represent date and time information.

strptime(string, format = None)
string A required string representation of the timestamp to be converted to struct_time
format An optional string detailing the format of the  string. 

The function returns the struct_time object representing the input time.

If the format is not given, the function will assume the default format,  '%a %b %d %H:%M:%S %Y' e.g Wed Feb 19 16:48:17 2020 .

Using the strptime() function.

#import time module
import time

date = 'Wed Aug  9 05:50:54 2023'

#Convert the time to a struct_time object
time_obj = time.strptime(date)


The time.ctime() function converts seconds representing a timestamp since the epoch to a string formatted similarly to the default format.

import time

today = time.ctime()
print(today, end = '\n')


with a custom format

We can use the format parameter to indicate to the strptime() function how the input time is presented The format parameter is a string that contains formatting codes that indicate for example,  if the date is presented as  "YYYY-MM-DD HH:MM:SS" then the format string should be "%Y-%m-%d %H:%M:%S".

using a custom format

import time

date = "14-3-2018 14:30:00"

format_string = "%d-%m-%Y %H:%M:%S"

time_obj = time.strptime(date, format_string)
import time

date = "July 6, 2023 13:30"

format_string = "%B %d, %Y %H:%M"

time_obj = time.strptime(date, format_string)