There are a number of ways we can take to get current time in Python.
- Using the
datetimeobject - Using the
timemodule
Current time using the datetime object
Output
Current Time = 07:41:19
In the above example, we have imported the datetime class from the datetime module.
Then, we used the now() function to get a datetime object containing current date and time.
Using datetime.strftime() function, we then created a string representing current time.
Current time using time module
In Python, we can also get the current time using the time module.
Output
07:46:58
Current time of a Certain timezone
If we need to find the current time of a certain timezone, you can use the pytZ module.
Output
NY time: 03:45:16 London time: 08:45:16
Here, we have used the pytz module to find the current time of a certain time zone.