How to Get Current Time in Python?
|In this Python article, we will discuss how to get only the current time using different methods and approaches. Let’s begin.
Before learning how to get the current date and time in python, we must learn the DateTime module in python.
There are different ways through which we can get the time. We will discuss three main ways of getting the current time. Generally, the datetime
module is used to create the object containing time and we will use pytz module to handle the timezone.
1. Get the Current time for a timezone (pytz module)
We can use the pytz library to find/get the current date and time for a specific timezone.
Let’s implement an example to find the current time in Kolkata-India, New York, and London.
from datetime import datetime import pytz set_timezone1 = pytz.timezone('Asia/Kolkata') set_timezone2 = pytz.timezone('America/New_York') set_timezone3 = pytz.timezone('Europe/London') time1 = datetime.now(set_timezone1).time() time2 = datetime.now(set_timezone2).time() time3 = datetime.now(set_timezone3).time() print("Current Time in Kolkata:", time1) print("Current Time in New York:", time2) print("Current Time in London:", time3)
Output Current Time in Kolkata: 01:10:38.255712 Current Time in New York: 15:40:38.255732 Current Time in London: 20:40:38.255737
2. How to Get Current Time using datetime
Object?
If we just want the string representation of the time then we can use strftime for the datetime object. This way we can format and just get the time information from the object.
from datetime import datetime as dt currenttime_now = dt.now() time_now = currenttime_now.strftime("%H:%M:%S") print("Current Time is:", time_now)
Output Current Time is: 21:46:06
Another way is to call time()
function on the datetime object which returns an instance of class datetime.time
.
from datetime import datetime as dt currenttime_now = dt.now() print("Current time:", currenttime_now.time()) print("Current time class info:", currenttime_now.time().__class__)
Output Current time: 21:46:06.651371 Current time class info: <class 'datetime.time'>
3. How to Get Current Time using the Time module?
Getting time using the time module is the simplest and easy way to get the time. It provides a lot of attributes to get detailed information separately.
import time current_time = time.localtime() print(current_time) print("hour :", current_time.tm_hour) print("min :", current_time.tm_min) print("sec :", current_time.tm_sec) print("wday :", current_time.tm_wday) print("zone :", current_time.tm_zone) print("--------------------------------------") current_time_str = time.strftime("%H:%M:%S", current_time) print(current_time_str)
Output time.struct_time(tm_year=2021, tm_mon=8, tm_mday=8, tm_hour=21, tm_min=52, tm_sec=37, tm_wday=6, tm_yday=220, tm_isdst=1) hour : 21 min : 52 sec : 37 wday : 6 zone : CEST -------------------------------------- 21:52:37
4. Conclusion
Finally, to sum up, in this article we have covered the methods of getting only the current time in python along with implementation of different methods, we have covered:
- How to get the current time of a timezone(pytZ module)?
- How to get current time using datetime object?
- How to get current time using the Time module?
Helpful Links
Please follow the Python tutorial series or the menu in the sidebar for the complete tutorial series.
Also for examples in Python and practice please refer to Python Examples.
Complete code samples are present on Github project.
Recommended Books
An investment in knowledge always pays the best interest. I hope you like the tutorial. Do come back for more because learning paves way for a better understanding
Do not forget to share and Subscribe.
Happy coding!! ?