What’s New ?

The Top 10 favtutor Features You Might Have Overlooked

Read More

How to Get Current Time in Python? (All Examples)

  • Apr 07, 2023
  • 6 Minute Read
  • Why Trust Us
    We uphold a strict editorial policy that emphasizes factual accuracy, relevance, and impartiality. Our content is crafted by top technical writers with deep knowledge in the fields of computer science and data science, ensuring each piece is meticulously reviewed by a team of seasoned editors to guarantee compliance with the highest standards in educational content creation and publishing.
  • By Komal Gupta
How to Get Current Time in Python? (All Examples)

Python has grown in popularity as a programming language for a wide range of jobs, but working with time is one job in which it excels. In this article, we'll look at some of the ways to get the current time in Python.

How to get Current Time in Python?

Here we will learn how to discern current time attributes such as the year, minutes, and seconds. We will also look into printing choices with time formats, how computers represent time, and how to cope with time zones.

Python includes a datetime.now() method that is used to get the current time. It is a part of the built-in datetime module that simplifies dealing with dates and times. This will print the current date and time in the style YYYY-MM-DD HH:MM:SS.sssss.

Here is the syntax for this purpose:

import datetime
current_time = datetime.datetime.now()
print(current_time)

 

In the above example, we have imported the datetime class and then we used the now() function to get a datetime object containing the current date and time. 

If you want to print the current time in a specific format, you can use the strftime() function. The below code with print time in the format HH:MM:SS.

import datetime
current_time = datetime.datetime.now()
formatted_time = current_time.strftime('%H:%M:%S')
print(formatted_time)

 

Printing the Current Timestamp

We can use the time module's time() function to print the current timestamp in Python. It will get the number of seconds (as a floating-point number) that have passed since January 1, 1970. This is the Unix approach as well.

Here is the code:

import time
current_timestamp = time.time()
print(current_timestamp)

 

We first call the time() function from the time module, which returns the current timestamp. We then print the current_timestamp variable using the print() function.

Current Time in Milliseconds

If you need the current time with millisecond precision, you can combine the datetime.now() function with the strftime() function.

Here is the code:

import datetime
current_time = datetime.datetime.now()
formatted_time = current_time.strftime('%Y-%m-%d %H:%M:%S.%f')
print(formatted_time[:-3])

 

How to get Current Time from the Internet?

Sometimes, we need to get the time from the internet to be more specific. You can use the requests module to make a call to a time API to get the current time from the internet in Python.

The following code, for example, utilizes the World Time API to obtain the current time in America:

import requests

response = requests.get('http://worldtimeapi.org/api/timezone/America/New_York')
json_response = response.json()
if 'utc_datetime' in json_response:
    current_time = json_response['utc_datetime']
    print(current_time)
else:
    print("No UTC datetime found in response")

 

This code will check if the "utc_datetime" key is present in the response, and if so, it will print out its value. Otherwise, it will print out a message saying that no UTC datetime was found in the response.

Another method for this is using the pytz library. You can use the datetime module to create a datetime object for the current time and then use the pytz module to convert this object to a specific time zone.

Learn with this example:

from datetime import datetime
import pytz
# Get the timezone object for London
timez= pytz.timezone('Europe/London')
datetime_London = datetime.now(timez)
print(datetime_London)

 

The pytz is a third-party module that helps with time zones. It has an accurate database of time zones, as well as functions for converting between different time zones. To use pytz, you must first install it by running pip install pytz in your command prompt or terminal.

Also, learn how to get the current year in Python.

Conclusion

We now covered everything with getting current time in Python, which is very simple with the datetime module. Happy Learning :)

FavTutor - 24x7 Live Coding Help from Expert Tutors!

About The Author
Komal Gupta
I am a driven and ambitious individual who is passionate about programming and technology. I have experience in Java, Python, and machine learning, and I am constantly seeking to improve and expand my knowledge in these areas. I am an AI/ML researcher. I enjoy sharing my technical knowledge as a content writer to help the community.