In this article, we will see how to get current time. If you are looking for “How to print time in python”, this article will help you for the same.
1. Using datatime object
Here, we are importing
datetime class from the datetime module. And we use
now() method to get a
datetime object containing current date and time. Using datetime.strftime() method, we then created a string representing current time.
from datetime import datetime now = datetime.now() current_time = now.strftime("%H : %M : %S") print("Current time is ", current_time)
Current time is 10 : 47 : 57
2. Using time module
import time now = time.localtime() current_time = time.strftime("%H : %M : %S", now) print("Current time is ", current_time)
Current time is 10 : 51 : 41