How to get current time in Python?

Python | Get current time: In this tutorial, we will learn how to get the current time using Python program? By IncludeHelp Last updated : April 21, 2023

In Python, we can get the current time by using different methods. We are discussing three methods here.

Getting current time in Python

To get the current time, you can use localtime() method of time module, now() method of datetime module, and the time which is based on the different timezones use the pytz module.

1. Get current time using time module

The following are the steps to get the current time using time module:

  • Import the time module.
    import time
  • Call the localtime() function of the time class – assign it in its object.
    time_object = time.localtime()
    
  • Format the time using strftime() function.
    current_time = time.strftime("%H:%M:%S", time_object)
    

Example 1

# Current time using time module

# importing the time module
import time

# getting the current time
time_object = time.localtime()

# format the time 
current_time = time.strftime("%H:%M:%S", time_object)

# print the time 

print("Current time is: ", current_time)

Output

Current time is:  08:04:32

2. Get current time using datetime object

The following are the steps to get the current time using datetime object:

  • Import the datetime class from datetime module.
    from datetime import datetime
    
  • Call the now() function of datetime class.
    obj_now = datetime.now()
    
  • Pass the object in the strftime() function to format the time.
    current_time = obj_now.strftime("%H:%M:%S")
    
  • Print the time.

Example 2

# Current time using datetime object

# importing the datetime class
# from datetime module
from datetime import datetime

# calling the now() function
obj_now = datetime.now()

# formatting the time
current_time = obj_now.strftime("%H:%M:%S")
print("Current time is:", current_time)

Output

Current time is: 08:08:53

3. Getting current time of different time zones

The following are the steps to get the current time of different time zones:

  • Import the datetime class from datetime module.
    from datetime import datetime
  • Importing the pytz module to access date time from a specified time zone.
    import pytz
  • Using timezone() function of pytz module – we can specify the time zone, using "now()" function – we can access the current time and using strftime() function – we can format the time.
    tz_IN = pytz.timezone('Asia/Calcutta') 
    datetime_IN = datetime.now(tz_IN)
  • Print the time.

Example 3

# Current time of different time zones

# importing the datetime class from 
# datetime module
from datetime import datetime
# importing the pytz 
import pytz

# specifying the indian time zoe 
tz_IN = pytz.timezone('Asia/Calcutta') 
datetime_IN = datetime.now(tz_IN)
print("Asia/Calcutta time is: ", datetime_IN.strftime("%H:%M:%S"))

# specifying the America/Chicago time zone
tz_Chicago = pytz.timezone('America/Chicago')
datetime_Chicago = datetime.now(tz_Chicago)
print("America/Chicago time is: ", datetime_Chicago.strftime("%H:%M:%S")) 

Output

Asia/Calcutta time is:  13:55:15
America/Chicago time is:  03:25:15

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.