Get Current Time In Python

Python provides the datetime module in order to work with date and time information. This module provides a lot of different methods and one of them is getting the current time. In this tutorial, we will learn how to get the current time for the local and another timezone in Python.

Get Current Time with datetime.now() Method

The most popular method to get the current time is using the now() method from the datetime module. First, we will import the datetime module and then call the now() method. It will return the time object which contains current time information.

from datetime import datetime

#Get current time
now = datetime.now()

#Print current time
print("Current Time is: ",now)

#Format current time 
print("Current Time is: ",now.strftime("%H:%M:%S"))

The output will be like below where the first print will print the complete date and time with the year, month, day, hour, minute, and second information. The second print will provide only the hour, minute, and second information by using the strftime() method with the “%H:%M:%S” format.

Current Time is: 2020-11-28 11:12:01.561611
Current Time is: 11:12:01

Get Current Time with time.localtime() Method

Python also provides a separate module named time which also provides the localtime() method which will return the current localtime information as localtime object.

import time

#Get current time
now = time.localtime()

#Print current time
print("Current Time is: ",now)

#Format current time 
print("Current Time is: ",time.strftime("%H:%M:%S",now))

The output will be like the below. The localtime() can be printed with print() method which will print the struct_time object and provides the time as parameters like below. The strftime() method can be called via the time module and the current time object now should be provided like time.strftime(“%H:%M:%S”,now) .

Current Time is: time.struct_time(tm_year=2020, tm_mon=11, tm_mday=28, tm_hour=11, tm_min=16, tm_sec=57, tm_wday=5, tm_yday=333, tm_isdst=0)
Current Time is: 11:16:57

Get Current Time For Different TimeZone

The pytz is a 3rd party Python module that uses the Olson tz or timezone data in order to return time for a specified timezone. The current time can be returned with the pytz timezone method and using datetime.now() method by providing the timezone object as the parameter.

from datetime import datetime
import pytz

tz_istanbul = pytz.timezone("Europe/Istanbul")
now = datetime.now(tz_istanbul)
print("Istanbul Time:",now.strftime("%H:%M:%S"))

tz_chicago = pytz.timezone("America/Chicago")
now = datetime.now(tz_chicago)
print("Chicago Time:",now.strftime("%H:%M:%S"))

tz_london = pytz.timezone("Europe/London")
now = datetime.now(tz_london)
print("London Time:",now.strftime("%H:%M:%S"))
Istanbul Time: 09:56:45
Chicago Time: 01:57:31
London Time: 07:57:31

Change Format of Current Time

As we can see in previous examples the format of the current time can be changed by using the strftime() method. This method is provided with the datetime module or with the time object. Using the time object strftime() method is easier. The format should be provided as string like “%H:%M:%S”.

from datetime import datetime

#Get current time
now = datetime.now()


#Format current time 
print("Current Time is: ",now.strftime("%H:%M:%S"))

print("Current Time is: ",now.strftime("%H:%M"))

print("Current Time is: ",now.strftime("%H-%M-%S"))

print("Current Time is: ",now.strftime("%H-%M"))

The output will be like the below.

Current Time is: 11:32:16
Current Time is: 11:32
Current Time is: 11-32-16
Current Time is: 11-32

Leave a Comment