How to Get Current Date and Time in Python?

In Python, to deal with time and date the “datetime” module and “time” module are used. Using these modules users can perform certain operations on date and time. For instance, to get the current date and time the “datetime.now()”, “date.today()” “time.time()”, etc. are used in Python.

In this tutorial, the below-listed methods are used to find the present/current date and time in Python.

Method 1: Using Datetime Module

The “datetime” module is used to deal with date and time in Python. It provides various functions to obtain the current date and time. Let’s see the following example to get the current date and time:

Example 1: Get Today Date Using date.today() Function

The “date.today()” function is used in the below code to get the current date:

Code:

import datetime
print(datetime.date.today())

We import the datetime module at the start of the program. The “datetime.date.today()” function returns the current date.

Output:

The current date has been shown.

Example 2: Get the Current Date and Time Using datetime.now() Function

The following code uses the “datetime.now()” function to get the current date and time:

Code:

import datetime
print(datetime.datetime.now())

The “datetime.now()” function is used to get the current date and time.

Output:

The present/current time and date have been shown on the screen.

Example 3: Get the Current/Present Date and Time Using Different Attributes

The following code is used to show the current date and time by using different attributes of the “datetime.now()” function:

Code:

import datetime
print('Current Date and Time: ', datetime.datetime.now())
print('Year: ', datetime.datetime.now().year)
print('Day: ',datetime.datetime.now().day)
print('Minute: ',datetime.datetime.now().minute)
print('Second: ',datetime.datetime.now().second)
  • The “datetime.datetime.now()” function is used to obtain the current/present time and date in Python. 
  • The attribute “.year”, “.day”, “.minute” and “.second” is used with the “datetime.datetime.now()” function to display the current year, day, minute, and seconds.

Output:

The present date and time along with different attributes have been returned in the output.

Example 4: Get Current Time in UTC and ISO Format

The UTC time format referred to as Coordinated Universal Time used with aviation or air traffic control, etc. The ISO format represents the current date and time in the “YYYY-MM-DDTHH:MM:SS. mmmmmm” format. 

Let’s understand how to use the UTC and ISO Format using the following code:

Code:

from datetime import datetime
print("UTC Time: ", datetime.utcnow())
print('\nCurrent ISO:', datetime.now().isoformat())

The “datetime.utcnow()” function is used to get the current date and time in “UTC” format. Similarly the “datetime.now().isoformat()” function is used to get the current date and time in “ISO” format.

Output:

The current time and date have been displayed in UTC and ISO format.

Method 2: Using time Module

The “time” module is used to get the current time in Python. Let’s see various examples to understand the usage of the current time in Python:

Example 1: Get Current Time Using time Module

The below code is used to get the current time using the “time.ctime()” and “time.localtime()” function:

Code:

import time
print('\nCurrent Time :', time.ctime(time.time()))
print('\nCurrent Time :', time.strftime("%H:%M:%S", time.localtime()))
  • The “time.ctime()” function accepts the “time.time()” function and returns the time in a human-readable format. 
  • Secondly, we use the “time.strftime()” function that accepts the string format and the “time.localtime()” function as an argument to return the current time.

Output:

The current time has been displayed successfully.

Example 2: Get the Current Time in GMT and Epoch

The below code uses the “time.gmtime()” and “time.time()” functions to get the GMT and Epoch time:

Code:

import time
print('Current GMT Time:', time.gmtime(time.time()))
print("\nEpoch Time is : ", int(time.time()))

The “time.gmtime()” accepts the “time.time()” function and returns the current time in GMT format. Next, we use the “time.time()” function along with “int()” to return the epoch time in integer format.

Output:

The GMT time and Epoch time have been displayed successfully.

Conclusion

To obtain the current/present date and time in Python, different functions of the datetime and time modules are used, such as date.today(), datetime.now(), etc. The Python “datetime” module functions such as “date.today()” and “datetime.now()” are used to get the present date and time. The time module functions such as “time.time()” and “time.localtime()” returns the current/present time in Python. This guide offers various techniques for obtaining the current date and time.