Piton

Python Get the Current Time

Python Get the Current Time

Python is a versatile, general-purpose, and high-level programming language of modern times. It offers many built-in modules and functions to perform some specific tasks. It also provides datetime and time modules to perform time-related tasks. The time value starts from January 1st, 1970, and most of the operating systems support the time value until 2038. The second is the unit of time calculation. The time value is returned in floating-point numbers. This article explains various ways to get the current time using Python.

Methods to get the current time

Following are the two methods to get the current time in Python:

  1. By using the datetime module
  2. By using the time module

Using the datetime module

The datetime is the built-in Python module. It provides many built-in functions to get the current date and time. The now() function of the datetime module yields the current time, along with the date.

Let's use the now() function in our Python script to obtain the current time and date.

#importing the datetime module
from datetime import datetime
#using the now function
print("The current date and time is: ",datetime.now())

Output

The output displays the current date and time.

We can use the strftime() function to convert the datatime object value into a string. The directives are passed to the strftime() function as arguments, and it returns the value. For example, the directive %H, %M, and %S returns the value of hours, minutes, and seconds in a string format, respectively. Let's use these directives with the strftime() function to obtain the current time. In the given example, the output of the datetime.now() function is stored in the date_time variable. Moreover, the output of the strftime() function is stored in the current_time variable, and lastly, we are printing the current_time variable to get the current time value.

#importing the datetime module
from datetime import datetime
#using the now function to get the date and time value
date_time =datetime.now()
#using strftime() to get the current time value
current_time = date_time.strftime("%H:%M:%S")
print("The current time is: ",current_time)

Output

The output shows the current time value.

Instead of getting both the date and time value, we can also just get the time value. The now().time() function returns the time value only. The return value of datetime.now().time() is stored in current_time variable. Finally we are printing the current_time variable to get the current time value.

#importing the datetime module
from datetime import datetime
#using the time function to get the current time value
current_time =datetime.now().time()
print("The current time is: ",current_time)

Output

The output shows the current time value.

The datetime module can be used in different ways to get the current time value.

Using the time module

The current time can be obtained by using the localtime() function of the time module. Let's use the localtime() function in our Python script to get the current time value.

#importing the time module
import time
#using localtime() function to get the current time value
current_time = time.localtime()
#printing the current_time
print(current_time)

Output

The localtime() function returns the complete time structure.

We can get the current time value in an hour, minute, and second format by using the struct_time fields. The tm_hour returns the hour value. whereas the tm_min, and tm_sec returns the minutes and seconds values, respectively.

#importing the time module
import time
#using localtime() function to get the current time value
current_time = time.localtime()
#printing the current_time
print(current_time.tm_hour,":",current_time.tm_min,":",current_time.tm_sec)

Output

The strftime() function also returns the current time. The strftime() function takes the time structure as an argument along with the directives.

#importing the time module
import time
#using localtime() function to get the current time value
current_time_struct = time.localtime()
#using strftime() function
current_time = time.strftime("%H:%M:%S",current_time_struct)
print("The current time is: ",current_time)

Output

Conclusion

Python provides datetime and time module to perform the time-related tasks. We can get the current by using these two modules. This article explains the use of datetime and time module to get the current time with simple examples.

Motoare de jocuri gratuite și open source pentru dezvoltarea jocurilor Linux
Acest articol va acoperi o listă de motoare de jocuri gratuite și open source care pot fi utilizate pentru dezvoltarea jocurilor 2D și 3D pe Linux. Ex...
Tutorial Shadow of the Tomb Raider pentru Linux
Shadow of the Tomb Raider este a douăsprezecea completare a seriei Tomb Raider - o franciză de jocuri de acțiune-aventură creată de Eidos Montreal. Jo...
Cum se mărește FPS în Linux?
FPS înseamnă Cadre pe secundă. Sarcina FPS este de a măsura rata de cadre în redările video sau în performanțele jocului. În cuvinte simple, numărul d...