Open In App

Get Current Time in Python

Last Updated : 25 Oct, 2025
Comments
Improve
Suggest changes
17 Likes
Like
Report

Given a requirement to get the current system time in Python, there are several ways to achieve this. Let’s explore them.

Using datetime.now() and strftime()

This method uses datetime module, which work with both date and time. datetime.now() fetches the current local date and time, while strftime() allows formatting the time into a readable string format.

Python
from datetime import datetime
now = datetime.now()
t = now.strftime("%H:%M:%S")
print("Current Time =", t)

Output
Current Time = 14:17:03

Explanation:

  • datetime.now() returns a datetime object with the current local date and time.
  • strftime("%H:%M:%S") formats the time into hours, minutes, and seconds.

Using datetime.now() with pytz

This method is ideal when you want to get the current time for a specific time zone (for example, India, US, UK, etc.). The pytz module provides accurate and up-to-date timezone definitions that help display the correct time across regions.

Python
from datetime import datetime
import pytz

tz = pytz.timezone('Asia/Kolkata')
dt = datetime.now(tz)
print("India Time:", dt.strftime("%H:%M:%S"))

Output
India Time: 19:49:44

Explanation:

  • pytz.timezone('Asia/Kolkata') creates a timezone object for India.
  • datetime.now(tz) returns the current date and time in the given timezone.
  • strftime() formats the datetime object into a string showing only the time.

Using time module

The time module provides functions to work with time values in seconds and to format them into readable formats. It is mainly used for timing operations or measuring code execution durations.

Python
import time
t = time.localtime()
c = time.strftime("%H:%M:%S", t)
print("Current Time:", c)

Output
Current Time: 14:21:55

Explanation:

  • time.localtime() gets the current local time as a struct_time object.
  • time.strftime("%H:%M:%S", t) converts the struct_time object into a formatted string.

Explore