Python Get the Current Time

Python logo

In Python, time are not a data type of its own, but a module named time can be imported to work with the time. There are a number of ways you can get current time in Python, but in this tutorial we just import the time module to get the current time. I also recommend checking out the datetime module. Maybe we’ll do a more extensive article on that module as well.

Example 1: Current time using time module

First we need to import date module:

import time

Then we call the function Print to print the current time.

print(time.asctime())

Then we get the current datetime:

Output:

Fri May 21 20:40:20 2021

Example 2: Current time using time module

We can get only current time too:

import time

 t = time.localtime()
 current_time = time.strftime("%H:%M:%S", t)
 print(current_time)

Output:

20:40:20

Conclusion

This article explains the use of time module to get the current time with two simple examples.


If you like this blog post and it was useful to you, please follow us on Twitter and Facebook.



Related articles

Sources:

Post a Comment

0 Comments