Python Get Current Time

Python is a modern programming language that is adaptable, general-purpose, and high-level. It also has datetime and time modules for performing time-sensitive activities. The time value begins on January 1st, 1970, and most operating systems support it until 2038. Working with time is an important aspect of many applications. Assume you’re creating a sign-up form for a website. You might wish to get the current time so you know when a user joined your site. This is where the Python get current time module comes in handy. You can use the time module in your Python scripts to work with time.

Python Date and Time

Date and time are not data types in Python, however, a module called datetime can be imported to work with both the date and the time. There is no need to install the datetime module outside because it is built into Python. In Python, there are numerous ways to obtain the current date and time by utilizing built-in and third-party modules.

Methods to get current time

The two techniques for obtaining the current time in Python are as follows:

  • By making use of the datetime module
  • By employing the time module

Using the datetime module

The datetime module is a built-in Python module. It has a plethora of built-in capabilities for obtaining the current date and time. The datetime module’s now() function returns the current time as well as the date. The datetime.now() function returns the current local time and date. It displays datetime in the YYYY-mm-dd hh:mm:ss.microseconds format by default.

To import the datetime module we using the following method:


from datetime import datetime

Example 1: Current time using datetime object



# Python program to illustrate Python get current time
# Importing datetime module
from datetime import datetime
# storing the current time in the variable
c = datetime.now()

# Displays Time
current_time = c.strftime('%H:%M:%S')
print('Current Time is:', current_time)
# OR
# Displays Date along with Time
print('Current Date and Time is:', c)



Current Time is: 06:50:10
Current Date and Time is: 2021-09-24 06:50:10.535005


To obtain the current date and time, we implement the datetime.now() in the above program. Then, using the now() method, we obtained a datetime object with the current date and time. However, we simply want the current time, so we used strftime(). We then constructed a string indicating the current time using the datetime.strftime() method. We then use %H to extract hours, %M to extract minutes, and %S to extract seconds.

Another method to extract current time without using the strftime() method is by using the time() function. To obtain the current time in seconds since the epoch as a floating-point value, use the time() function.



# Python program to illustrate Python get current time
# Importing datetime module
from datetime import datetime

# storing the current time in the variable
c = datetime.now().time()  # time object

# Displays Time
print('Current Time is:', c)
print('Type(c):', type(c))



Current Time is: 06:57:31.896091
Type(c): <class 'datetime.time'>

Using the time module

You can use the time module to implement a variety of time-related functions in your Python scripts. The time module is available in all versions of Python. We must first import it into our program. We may accomplish this with a Python import statement:

The time module’s localtime() function can be used to determine the current time. To obtain the current time value, let us utilize the localtime() function in our Python script.

Example 2: Current time using time module



# Python program to illustrate Python get current time
import time

# Local time has date and time
t = time.localtime()
print('Local Time is ', t)

# Extract the time part
current_time = time.strftime("%H:%M:%S", t)
print('Current time is ', current_time)



Local Time is  time.struct_time(tm_year=2021, tm_mon=9, tm_mday=24, 
tm_hour=8, tm_min=12, tm_sec=56, tm_wday=4, tm_yday=267, tm_isdst=0)
Current time is  08:12:56


Using the struct_time attributes, we can obtain the current time value in hour, minute, and second forms. The tm_hour function returns the hour value. The tm_min and tm_sec, on the other hand, yield the minutes and seconds values, respectively.

The following are the values that the struct_time object can store:

  • tm_year: the year
  • tm_mon: month
  • tm_mday: day
  • tm_hour: hour
  • tm_min: minute
  • tm_sec: second
  • tm_wday: weekday (0 is Monday)
  • tm_yday: year’s day
  • tm_isdst: time in daylight savings time

Example 3: Current time of a timezone

When you run the above programs, you will see that they do not provide you with the current date and time in your timezone. To obtain the date and time for a specific timezone, now() accepts timezone as an input and returns timezone-oriented output time. However, these time zones are defined in the pytz library.



# Python program to illustrate Python get current time of a timezone
from datetime import datetime
import pytz

tz_Mumbai = pytz.timezone('Asia/Kolkata')
datetime_Mumbai = datetime.now(tz_Mumbai)
print('India time:', datetime_Mumbai.strftime('%H:%M:%S'))

tz_Paris = pytz.timezone('Europe/Paris')
datetime_Paris = datetime.now(tz_Paris)
print('Paris time:', datetime_Paris.strftime('%H:%M:%S'))

tz_Dubai = pytz.timezone('Asia/Dubai')
datetime_Dubai = datetime.now(tz_Dubai)
print('Dubai time:', datetime_Dubai.strftime('%H:%M:%S'))



India time: 13:57:43
Paris time: 10:27:43
Dubai time: 12:27:43

Frequently Asked Questions

Q1. How do I get the current time in Python?

In Python, there are numerous ways to acquire the current time using built-in and third-party modules. The Python time module includes a number of functions for determining the current time and performing time-related tasks.



from datetime import datetime
import time

# Method 1
t = time.localtime(time.time())
print('Hours:', t.tm_hour)
print('Minutes:', t.tm_min)
print('Seconds:', t.tm_sec)

# Method 2
now = datetime.now()
print('Current Time:', now.time())

# Method 3
print('Current Time:', time.ctime())



Hours: 11
Minutes: 20
Seconds: 58
Current Time: 11:20:58.694902
Current Time: Fri Sep 24 11:20:58 2021

Q2. How do I get the current date in Python?

The Python Datetime module includes the Date class, which can be used to represent and manipulate dates. The Gregorian calendar is taken into account by the Date class.

  • Import the datetime module’s date class
  • To obtain the current date, use the date.today() function



from datetime import date

today = date.today()
print('Current Date:', today)



Current Date: 2021-09-24

Q3. Why is the pendulum module used for?

Pendulum is another Python tool that may be used to obtain the current time in various time zones. It is comparable to the pytz module, with the sole difference being that the pendulum module is faster. But first, we need to install the pendulum module in Python by using the following command in Terminal:


python3 -m pip install pendulum



from datetime import datetime
import pendulum

Ind = pendulum.timezone('Asia/Kolkata')
NY = pendulum.timezone('America/New_york')

print('Current Time in India:', datetime.now(Ind))
print('Current Time in New York:', datetime.now(NY))



Current Time in India: 2021-09-24 14:16:46.579459+05:30
Current Time in New York: 2021-09-24 04:46:46.621306-04:00

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.