Python program to get Current Time

In this article, we will know the approaches to get the current time in Python. There are multiple ways to get it. The most preferably date-time module is used in Python to create the object containing date and time.
1: Current time of a timezone – Using pytZ module

filter_none

edit
close

play_arrow

link
brightness_4
code

from datetime import *
import pytz
  
  
tz_INDIA = pytz.timezone('Asia/Kolkata'
datetime_INDIA = datetime.now(tz_INDIA)
print("INDIA time:", datetime_INDIA.strftime("%H:%M:%S"))

chevron_right


Output:

INDIA time: 19:29:53

2 : Current time – Using date time object

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

from datetime import datetime
  
# now() method is used to
# get object containing 
# current date & time.
now_method = datetime.now()
  
# strftime() method used to
# create a string representing
# the current time.
currentTime = now_method.strftime("%H:%M:%S")
print("Current Time =", currentTime)

chevron_right


Output:



Current Time = 19:31:51

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

from datetime import datetime
  
# Time object containing 
# the current time.
time = datetime.now().time() 
  
print("Current Time =", time)

chevron_right


Output:

Current Time = 19:33:29.087840

3. Getting Time – using Time module.

filter_none

edit
close

play_arrow

link
brightness_4
code

import time
  
  
# localtime() method used to
# get the object containing
# the local time.
Time = time.localtime()
  
# strftime() method used to
# create a string representing
# the current time.
currentTime = time.strftime("%H:%M:%S", Time)
print(currentTime)

chevron_right


Output:

19:35:17



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.