Program to get current time using Python

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:

Program to get current time Output

Approach 2: Using Date Time object

Program to get current time using Python

Output:

Program to get current time Output 2

Approach 3: Using Time

Program to get current time

Output:

Program to get current time Output 3

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.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.