Python | time.asctime() method

12 Oct, 2021
Python time method time.asctime() is used to convert a tuple or a time.struct_time object representing a time as returned by time.gmtime() or time.localtime() method to a string of the following form:
Day Mon Date Hour:Min:Sec Year
For example:
Thu 08 22 10:46:56 2019
time.asctime([t])
t (optional): A tuple or time.struct_time object representing a time as returned by time.gmtime() or time.localtime() method . If the ‘t’ parameter is not provided or None then the current time as returned by time.localtime() method is used.
This method returns an string of the form Day Mon Date Hour:Min:Sec Year 
Python3
# Python program to explain time.asctime() method # importing time moduleimport time  # Convert the current time# as returned by time.time() method# to a time.struct_time object in UTC# using time.gmtime() methodobj = time.gmtime() # Print time.struct_time objectprint("time.struct_time object as returned by time.gmtime() method:")print(obj) # Convert the time.struct_time# object to a string of the# form 'Day Mon Date Hour:Min:Sec Year'# using time.asctime() methodtime_str = time.asctime(obj) # Print the time.struct_time object# to the string of the# form 'Day Mon Date Hour:Min:Sec Year'print("\ntime.struct_time obj in string of the form \'Day Mon Date Hour:Min:Sec Year':")print(time_str)  # Convert the current time# as returned by time.time() method# to a time.struct_time object in local time# using time.localtime() methodobj = time.localtime() # Print time.struct_time objectprint("\ntime.struct_time object as returned by \time.localtime() method:")print(obj) # Convert the time.struct_time# object to a string of the# form 'Day Mon Date Hour:Min:Sec Year'# using time.asctime() methodtime_str = time.asctime(obj) # Print the time.struct_time object# to the string of the# form 'Day Mon Date Hour:Min:Sec Year'print("\ntime.struct_time obj in string of the form \'Day Mon Date Hour:Min:Sec Year':")print(time_str)
Output:
time.struct_time object as returned by time.gmtime() method:
time.struct_time(tm_year=2021, tm_mon=10, tm_mday=7, tm_hour=15, tm_min=30, tm_sec=51, tm_wday=3, tm_yday=280, tm_isdst=0)
time.struct_time obj in string of the form ‘Day Mon Date Hour:Min:Sec Year’:
Thu Oct  7 15:30:51 2021
time.struct_time object as returned by time.localtime() method:
time.struct_time(tm_year=2021, tm_mon=10, tm_mday=7, tm_hour=21, tm_min=0, tm_sec=51, tm_wday=3, tm_yday=280, tm_isdst=0)
time.struct_time obj in string of the form ‘Day Mon Date Hour:Min:Sec Year’:
Thu Oct  7 21:00:51 2021
Python3
# Python program to explain time.asctime() method # importing time moduleimport time  # If the parameter 't'# in time.asctime() method# is not provided then# the current time as# returned by time.localtime()# method is used # Convert the current time# as returned by time.localtime() method# to a string of the# form 'Day Mon Date Hour:Min:Sec Year'# using time.asctime() methodtime_str = time.asctime() # Print the stringprint(time_str)
Output:
Thu Oct  7 21:01:45 2021
Python3
# Python program to explain time.asctime() method # importing time moduleimport time  # A tuple with 9 attributes# representing a timet = (2019, 8, 22, 11, 21, 48, 3, 234, 0) # Convert the tuple# to a string of the# form 'Day Mon Date Hour:Min:Sec Year'# using time.asctime() methodtime_str = time.asctime(t) # Print the stringprint(time_str)
Output:
Thu Aug 22 11:21:48 2019
kumar_satyam
sooda367
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.