In this post we will be creating a Python script that will retrieve the current time using the ‘time’ module. The time module provides us with various time-related functions.
See the snippet of Python code below where we retrieve the time in seconds since the epoch. The epoch is where the time starts and is the date of January 1, 1970, 00:00:00 (UTC) on all platforms.
import time
current_time = time.time()
print(current_time)
An example of output of the above can be seen as.
1670940890.9022262
>>>
The above will return the number of seconds since the epoch, to return the time in a readable format we would use the snippet of code below.
import time
readable_time = time.ctime(current_time)
print(readable_time)
An example of output of the above can be seen as.
Tue Dec 13 14:14:50 2022
>>>
Take a look at some of our other content around the Python programming language by clicking here.