Get the current time Using the time Module
The Python time module, allows you to work with time in Python. It provides features such as retrieving the current time, pausing the program’s execution, and so on. So, before we begin working with this module, we must first import it.
Get the current time using the time
Here, we are getting the current time using the time module.
Python3
import time curr_time = time.strftime( "%H:%M:%S" , time.localtime()) print ( "Current Time is :" , curr_time) |
Output:
Current Time is : 16:19:13
Get Current Time In Milliseconds using time
Here we are trying to get time in milliseconds by multiplying time by 1000.
Python3
import time millisec = int ( round (time.time() * 1000 )) print ( "Time in Milli seconds: " , millisec) |
Output:
Time in Milli seconds: 1655722337604
Get Current Time In Nanoseconds using time
In this example, we will get time in nanoseconds using time.ns() method.
Python3
import time curr_time = time.strftime( "%H:%M:%S" , time.localtime()) print ( "Current Time is :" , curr_time) nano_seconds = time.time_ns() print ( "Current time in Nano seconds is : " , nano_seconds) |
Output:
Current Time is : 16:26:52
Current time in Nano seconds is : 1655722612496349800
Get Current GMT Time using time
Green Mean Time, which is also known as GMT can be used by using time.gmtime() method in python just need to pass the time in seconds to this method to get the GMT
Python3
import time # current GMT Time gmt_time = time.gmtime(time.time()) print ( 'Current GMT Time:\n' , gmt_time) |
Output:
Current GMT Time:
time.struct_time(tm_year=2022, tm_mon=6, tm_mday=20,
tm_hour=11, tm_min=24, tm_sec=59, tm_wday=0, tm_yday=171, tm_isdst=0)
Get Current Time In Epoch using time
It is mostly used in file formats and operating systems. We can get the Epoch current time by converting the time.time() to an integer.
Python3
import time print ( "Epoch Time is : " , int (time.time())) |
Output:
Epoch Time is : 1655723915
How to Get Current Date and Time using Python
In this article, we will cover different methods for getting data and time using the DateTime module and time module in Python.
Different ways to get Current Date and Time using Python
- Current time using DateTime object
- Get time using the time module