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 where one of them is getting the current time. In this tutorial, we will learn how to get the current time in Python.

Get Current Time with datetime.now() Method

The most popular method to get 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 complete date and time with year, month, day, hour, minute, 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 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 Specified TimeZone

The pytz is a 3rd party Python module which uses the Olson tz or timezone data in order to return time for specified timezone. The current time can be returned with the pytz timezone method and using datetime.now() method by providin the timzone object as 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"))

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 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