Home »
Python »
Python Programs
How to get current time in Python?
Python | Get current time: In this tutorial, we will learn how to get the current time using Python program?
By IncludeHelp Last updated : April 21, 2023
In Python, we can get the current time by using different methods. We are discussing three methods here.
Getting current time in Python
To get the current time, you can use localtime() method of time module, now() method of datetime module, and the time which is based on the different timezones use the pytz module.
1. Get current time using time module
The following are the steps to get the current time using time module:
Example 1
# Current time using time module
# importing the time module
import time
# getting the current time
time_object = time.localtime()
# format the time
current_time = time.strftime("%H:%M:%S", time_object)
# print the time
print("Current time is: ", current_time)
Output
Current time is: 08:04:32
2. Get current time using datetime object
The following are the steps to get the current time using datetime object:
Example 2
# Current time using datetime object
# importing the datetime class
# from datetime module
from datetime import datetime
# calling the now() function
obj_now = datetime.now()
# formatting the time
current_time = obj_now.strftime("%H:%M:%S")
print("Current time is:", current_time)
Output
Current time is: 08:08:53
3. Getting current time of different time zones
The following are the steps to get the current time of different time zones:
Example 3
# Current time of different time zones
# importing the datetime class from
# datetime module
from datetime import datetime
# importing the pytz
import pytz
# specifying the indian time zoe
tz_IN = pytz.timezone('Asia/Calcutta')
datetime_IN = datetime.now(tz_IN)
print("Asia/Calcutta time is: ", datetime_IN.strftime("%H:%M:%S"))
# specifying the America/Chicago time zone
tz_Chicago = pytz.timezone('America/Chicago')
datetime_Chicago = datetime.now(tz_Chicago)
print("America/Chicago time is: ", datetime_Chicago.strftime("%H:%M:%S"))
Output
Asia/Calcutta time is: 13:55:15
America/Chicago time is: 03:25:15