How To Get Current Time In Python – A Definitive Guide

In any application, getting current time is useful when you want to log the time of any specific event. For example, when a user is logged into the system is important to track the usage of the application. There are different ways to get the current time.

You can get the current time in python using datetime.now().strftime("%H:%M:%S") from the DateTime module.

In this tutorial, you’ll learn how to get the current time in python using the DateTime library and time module. You’ll also learn how to get different times in different formats.

If You’re in Hurry…

You can use the below code snippet to get the current time in python.

First, use the datetime.now() from the DateTime library and then format it using the strftime("%H:%M:%S") to get only the time information.

Snippet

from datetime import datetime

now = datetime.now()

current_time = now.strftime("%H:%M:%S")

print("Current Time is :", current_time)

When you print the current_time object, you’ll see the current time in the 24H format as shown below.

Output

    Current Time is : 18:59:27

If You Want to Understand Details, Read on…

In this tutorial, you’ll learn the different methods available to get the current time and how to format it in different ways.

Using Datetime Module

In this section, you’ll learn how to use the Datetime module to get the current time in python. It is available in python by default.

You can use the now() method from the Datetime object to get the current time. This returns the complete date and time information in your current Locale. After getting this, you can format the value using the strftime() method to fetch only the time information.

Use the below snippet to get the current time.

Snippet

from datetime import datetime

now = datetime.now()

current_time = now.strftime("%H:%M:%S")

print("Current Time is :", current_time)

When you print the current time object, you’ll get the time printed in the 24H format. To convert it to 12H format read this answer.

Output

    Current Time is : 18:59:27

This is how you can use the DateTime module to get the current time in python.

Using Time Module

In this section, you’ll learn how to get the current time using the time module. It provides various methods for time-related functionalities. It is available in python by default.

You can use the time.localtime() method to get the time from the time module. This will return the current time of your Locale.

This also returns the complete date and time information which can be formatted using the strftime() to display only the time as shown below.

Snippet

import time

tim = time.localtime()

current_time = time.strftime("%H:%M:%S", tim)

print("Current Time is :", current_time)

When you print the current time, you can see that the current time is printed in a 24H format. To convert it to 12H format read this answer.

Output

    Current Time is : 18:59:28

This is how you can get the current time in your locale using the time module.

Get Current Time Of a Timezone

You can get the current time of a specific timezone by using the pytz module and the DateTime module.

This library is available in Python since version 2.4. However, if it’s not available, you can install it by using the following code snippet.

pip install pytz

You can check out all the available time zones by using the below snippet.

Snippet

pytz.all_timezones

You’ll see the available time zones as a list.

Output

['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa',... 'UTC', 'Universal', 'W-SU', 'WET', 'Zulu']

To get the current time of a timezone, you need to create a timezone object by using the pytz.timezone() method and pass it to the datetime.now() method. Then, it’ll return the current time of that specific timezone.

For example, to get the time of the timezone Asia/Kolkata, use the below code.

Snippet

from datetime import datetime

import pytz

tz_IN = pytz.timezone('Asia/Kolkata') 

datetime_IN = datetime.now(tz_IN)

print("Current time in India is :", datetime_IN.strftime("%H:%M:%S"))

When you print the time, you’ll get the time of the specific timezone printed in 24H format.

Output

    Current time in India is : 18:59:28

This is how you can get the current time of a specific timezone.

Get Current Time in Milliseconds

Getting current time in milliseconds can be useful when you need to track the time in most granular details.

You can get the current time in milliseconds in python by multiplying the current time X 100.

Use the below snippet to get the current time in milliseconds.

Snippet

import time

milliseconds = int(round(time.time() * 1000))

print("Current time in Milli seconds is : ", milliseconds)

Output

    Current time in Milli seconds is :  1628947768368

This is how you can get the current time in milliseconds.

Get Current Time in Nanoseconds

Getting current time in nanoseconds is useful when you want to get time granular than that of the milliseconds.

You can get the current time in Nanoseonds by using the time.time_ns() method. It is available since python 3.7.

Use the below snippet to get the current time in nanoseconds.

Snippet

import time

nano_seconds = time.time_ns()

print("Current time in Nano seconds is : ", nano_seconds)

Output

    Current time in Nano seconds is :  1628947768487849100

This is how you can get the current time in python in nanoseconds.

Get Current Time in UTC

When you’re working with applications that have global users, it’s better to deal with the time in UTC format for logging the events. In this way, you can display the time inappropriate time zones of the specific users when accessing the logs.

What is UTC?

It is a Coordinated universal time standard by which the world regulates the clock and time.

You can get the current time in UTC by using the datetime.utcnow() method.

Use the below snippet to get the current time in UTC.

Snippet

from datetime import datetime

utc_time = datetime.utcnow()

print("UTC Time is : ", utc_time)

Output

    UTC Time is :  2021-08-14 13:29:28.621777

Get Current Time in Epoch

Epoch time is widely used in operating systems and file formats. An epoch is a date and time from which a computer measures system time.

You can get the current time in epoch by converting the time.time() to int as int(time.time()).

Use the below snippet to get the current time in the epoch.

Snippet

import time

epoch_time = int(time.time())

print("Current epoch is : ", epoch_time)

Output

    Current epoch is :  1628947768

Get Current Time in String Format

now() method in the DateTime module returns the time in the date-time format. You need to convert it into a string for logging it.

You can get the current time in string format by using the datetime.now().strftime(“%H:%M:%S”).

Use the below snippet to get the time in the String format.

Snippet

from datetime import datetime

now = datetime.now()

current_time = now.strftime("%H:%M:%S")

print("Current Time is :", current_time)

Output

    Current Time is : 18:59:28

Get Current Time in Hours

You can get the current time in hours by using the now.hour attribute of the DateTime object.

Use the below snippet to get the current time in hours.

Snippet

import datetime

now = datetime.datetime.now()

print("Current time in hours is : ", now.hour)

Output

    Current time in hours is :  18

Get Current Time in Minutes

You can get the current time in hours by using the now.minute attribute of the DateTime object.

Use the below snippet to get the current time in minutes.

Snippet

import datetime

now = datetime.datetime.now()

print("Current time in Minutes is : ", now.minute)

Output

    Current time in Minutes is :  59

Get Current Time in Seconds

You can get the current time in hours by using the now.second attribute of the DateTime object.

Use the below snippet to get the current time in seconds.

Snippet

import datetime

now = datetime.datetime.now()

print("Current time in Minutes is : ", now.second)

Output

    Current time in Minutes is :  29

This is how you can get the current time in minutes or hours or seconds.

Conclusion

To summarize, you’ve learned how to get the current time in python using the Datetime module or time module. You’ve also learned how to get the current time of a timezone, get the current time in UTC timezone to deal time in applications of global users.

Additionally, you’ve also learned, how to get time in the granular details lever such as milliseconds, nanoseconds, etc.

If you have any questions, comment below.

You May Also Like

Leave a Comment