How to get current time in seconds in Python

In this tutorial, we will discuss how to get the current time in seconds in Python.

In Python, there are many ways to get the current time. One such way is Time module. Time module has various functions to get the current time.

Also check:

Some of the time module functions are:

  • time.time()
  • time.ctime()
  • time.localtime()
  • time.monotonic()

Get current time in seconds in Python

Syntax:

time.time()

time.time() function gives current time in seconds since the epoch as a floating number.

Let’s see the code

# import the time module
import time

# get current time in seconds
t = time.time()
print('Time:', t)

# get current time in seconds as integer
s = int(round(time.time()))
print('Rounded Time:', s)

Output:

Time: 1643189517.22833
Rounded Time: 1643189517

In the above output,  there are two outputs which are exact time in the format of seconds.mircroseconds and the other in the format of seconds which is rounded as integer.

Also read:

Leave a Reply

Your email address will not be published.