How to Get the Current Time in Python?

[toc]

Introduction

As a programmer, you need to work on different time-based tasks in your program like logging and saving the records to the database and accessing the documents. In each of those cases, date and time assume a significant part in saving the importance and integrity of the data and information. So, you must know how to get the current time or date using Python.

In this article, we will see how to get the current time of your local time zone as well as the different time zones in Python.

Example: Before we proceed further, here’s a teaser of what is to follow next in this article.

# Importing datetime module
from datetime import datetime

# storing the current time in the variable
current = datetime.now()
print("Current time is", current)

Output:

Current time is 2021-06-30 18:10:47.005834

Interesting! Isn’t it? Hence without further delay, let us dive into the different methods that will allow you to get the current time in Python.

Method 1: Using The time Module

The simplest way to get the current time using Python is to use the time module. The time module in Python has a set of functions that allows us to work with problems that include time-trelated computations. Let’s find out what the official documentation says about the time module:

If you want to dive deep into the time module, please have a look at this illustrative guide to Python’s time module:- A Gentle Introduction to Python’s Time Module.

Now that we have an overview of the time module let’s learn the methods that will aid us to generate the current time.

Remember: You must import the time module before utilizing it.

strftime

strftime is a method of the time module in Python that converts a tuple or a struct_time to a string that is then used to represent the current time of your local time zone. The following example will help you to understand how this method works:

Example:

# Importing the time module
import time

# To get the time of your local timezone
current = time.localtime()

# Using strftime method
current_time = time.strftime("%H:%M:%S", current)
print("Current time is", current_time)

Output:

Current time is 18:36:02

ctime

By using the time.ctime(), we can get the current time in the way preferred by the operating system. This means, if we change our system time preferences, so will the output of the program. Hence, it is not standard across all systems.  

Example:

# Importing the time module
import time

# To get the time according to your operating system using ctime()
current = time.ctime()
print("Current time is", current)

Output:

Current time is Wed Jun 30 18:40:51 2021

🌍 Recommended: Ten Python One-Liners to Get Today’s Date as YYYY-MM-DD

Method 2: Using The datetime Object

Another module that is really handy when you want to play around with date-time functionalities in Python is the datetime module to get the current time.

Remember: You must import the module to utilize it.

from datetime import datetime

There are various classes inside the datetime module that store information about the date and time. For e.g. –

  • The datetime.time class is used to store the time in hours, minutes, seconds, and microseconds.
  • The datetime.date class stores the [day, month, year] or a date.

Example:

# Importing datetime module
from datetime import datetime

# Storing the current time in the variable
current = datetime.now()
print("Current time is", current)

Output:

Current time is 2021-06-30 18:55:22.133331

✨ The now() function is used to get the current time and date. We can also specifically get the year, month or even millisecond, etc. (attributes of date and time) when the variable was created. Let’s have a look at how?

Example:

# Importing datetime module
from datetime import datetime

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

#  Specific attributes
print("Current hour is", current.hour)
print("Current minute is", current.minute)
print("Current second is", current.second)
print("Current day is", current.day)
print("Current month is", current.month)
print("Current year is", current.year)

# Current date
print("Current date is", current.now().date())

Output:

Current hour is 20
Current minute is 32
Current second is 19
Current day is 30
Current month is 6
Current year is 2021
Current date is 2021-06-30

Method 3: Using The pytz Library

On the majority of the occasions, we may need to get the date and time in a particular time zone so that it can be utilized by others and not just in our time zone. Thus, pytz is one of the well-known Python modules that can be utilized to get the time at a particular time zone.

Since pytz is not a part of Python’s internal libraries, we need to install it in order to utilize it. To install it, use the following command:

pip install pytz

Now let’s have a look at an example to understand how we can utilize the ‘pytz‘ module to work with time zones.

Example:

# Importing pytz and datetime modules
from datetime import datetime
import pytz

# Time at various timezones using pytz
ny = pytz.timezone('America/New_york')
par = pytz.timezone('Europe/Paris')
tim = pytz.timezone('Africa/Timbuktu')
dub = pytz.timezone('Asia/Dubai')
can = pytz.timezone('Australia/Canberra')

print('Current Time in New York:', datetime.now(ny))
print('Current Time in Paris:', datetime.now(par))
print('Current Time in Timbuktu:', datetime.now(tim))
print('Current Time in Dubai:', datetime.now(dub))
print('Current Time in Canberra:', datetime.now(can))

Output:

Current Time in New York: 2021-06-30 11:09:43.273780-04:00
Current Time in Paris: 2021-06-30 17:09:43.273780+02:00
Current Time in Timbuktu: 2021-06-30 15:09:43.273780+00:00
Current Time in Dubai: 2021-06-30 19:09:43.273780+04:00
Current Time in Canberra: 2021-07-01 01:09:43.273780+10:00

That’s cool! Isn’t it? ? To know all the time zones supported by the pytz module you can use the following command:

import pytz
print(pytz.all_timezones)

Method 4: Using The Pendulum Module

Pendulum is another Python module used to get the current time of different time zones. It is similar to the pytz module, with the only difference being that; pendulum module is faster than the pytz module.

Once again, you have to install and import the pendulum module before utilizing it using pip.

Example:

# Importing pendulum and datetime modules
from datetime import datetime
import pendulum

# Time at various timezones using pendulum
ny = pendulum.timezone('America/New_york')
par = pendulum.timezone('Europe/Berlin')
cal = pendulum.timezone('Asia/Calcutta')

print('Current Time in New York:', datetime.now(ny))
print('Current Time in Paris:', datetime.now(par))
print('Current Time in Calcutta:', datetime.now(cal))

Output:

Current Time in New York: 2021-06-30 11:29:24.645318-04:00
Current Time in Paris: 2021-06-30 17:29:24.645318+02:00
Current Time in Calcutta: 2021-06-30 20:59:24.645318+05:30

Conclusion

Here, we studied different methods that can be used to get the current time in Python. While the time() module is the most straightforward method, we cannot use it to get the time of different time zones. datetime() module also creates DateTime objects in our local time zone by default. Using it with the pytz() and the pendulum() modules help us to get the time of different time zones.

Post Credits
SHUBHAM SAYON
RASHI AGARWAL


  • One of the most sought-after skills on Fiverr and Upwork is web scraping. Make no mistake: extracting data programmatically from websites is a critical life skill in today’s world that’s shaped by the web and remote work.
  • So, do you want to master the art of web scraping using Python’s BeautifulSoup?
  • If the answer is yes – this course will take you from beginner to expert in Web Scraping.