time.asctime() method

time.asctime() method 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

Example: Converting tuple to time.struct_time object to string

This code uses the time module to convert a specified timestamp (1627987508.6496193) into a human-readable date and time format using time.asctime(). It does so both for the GMT (Greenwich Mean Time) timezone and the local timezone.

Python3




import time
obj = time.gmtime(1627987508.6496193)
time_str = time.asctime(obj)
print(time_str)
obj = time.localtime(1627987508.6496193)
time_str = time.asctime(obj)
print(time_str)


Output

Tue Aug  3 10:45:08 2021
Tue Aug  3 10:45:08 2021

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

...