Open In App
Related Articles

Python program to get Current Time

Improve Article
Improve
Save Article
Save
Like Article
Like

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.

Example 1: Current time of a timezone – Using pytZ module 

Python3




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"))


Output:

INDIA time: 19:29:53

Example 2 : Current time – Using date time object

Python3




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)


Output:

Current Time = 19:31:51

Time complexity: O(1).

Space complexity: O(1).

Example 3: 

Python3




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


Output:

Current Time = 19:33:29.087840

Example 4. Getting Time – using Time module. 

Python3




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)


Output:

19:35:17


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 28 Aug, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials