Table of Contents
Introduction
Python provides so many options to get the current time. Here, we will look through the various options provided.
Program
Approach 1: Using pytz
from datetime import * import pytz time_INDIA = pytz.timezone('Asia/Kolkata') datetime = datetime.now(time_INDIA) print("INDIA time is: ", datetime.strftime("%H:%M:%S"))
Output:
Approach 2: Using Date Time object
Output:
Approach 3: Using Time
Output:
Explanation
In the above program we can observe that to get the specific region current time we have used pytz module in approach 1. Python has provided datetime and time modules to get the current time. In approach 3, time.localtime() accepts the object containing the local time in seconds since epoch and returns local time.
0 Comments