Answer: To get the current time in Python, you can use the datetime.now() method.
Getting the current time is important because we need to record it for timestamps, logging, or scheduling a program. In this article, we will explore methods for getting the current time in Python.
Table of Contents:
Methods to Get the Current Time in Python
Python provides certain methods to print the current time. Let’s see these methods to get the current time with the help of examples:
Method 1: Using datetime.now() Method in Python
The datetime.now() method will help print the time when the program is executed. It helps to display the exact time when the code has been compiled.
Example:
In this code, we can edit the format of the time that has to be displayed if you want to print the minutes first, then change the format to “%M:%H:%S”. Likewise, we can change the format of the time displayed.
Output:
1. Based on attributes of datetime.now() method
Many attributes can be displayed, like day, month, year, minute, seconds, and hour. Based on these, we can see an example:
Example:
Output:
2. Using pytz and datetime module in datetime.now() method
In the last example, we can only see the date, time, hour, and minutes, but it doesn’t show the place or the timezone. We need to use a timezone-oriented library called ‘pytz’ to display the timezone along with the date and time.
Example:
Output:
3. Display UTC Date and Time using utcnow() method
The abbreviation of UTC is Coordinated Universal Time; this UTC helps the global user to log the user time.
Example:
Output:
Method 2: Using time() module to Display the Current Time in Python
To get the current time, we can use the time module in Python, then the libraries like time.gmtime() or time.localtime() for getting time in UTC or local time, respectively.
Example:
Output:
The GMT is abbreviated as Greenwich Mean Time. To get the current time in GMT format using the time module, we use time.gmtime(), which gives us the time in UTC.
Example:
In this example, you can see that time.gmtime() will give you the time in UTC, time.strftime(‘%a, %d %b %Y %H:%M:%S GMT’, current_time_gmt) formats the time in the format of: Day, DD Mon YYYY HH:MM:SS GMT.
Output:
Same as in the previous example, we can create an ISO format of time using iso_format_time. Here is an example:
Example:
In this example, the time has been converted from GMT to ISO.
Output:
3. To Display milliseconds in time()
Now, we can try displaying the current time including milliseconds using the time module. You can use time.time() to get the current time in seconds and then format it to include milliseconds.
Example:
Output:
4. To Display nanoseconds in time()
To display nanoseconds using the time module, you can use time.time() for seconds, but it doesn’t provide nanoseconds directly. For precise time measurement with nanosecond precision. However, the time module has time_ns() which returns the current time in nanoseconds.
Example:
Output:
Method 3: Using strftime() method to Display the Current Time in Python
While strftime() does not directly support nanoseconds, you can format the time with milliseconds from datetime, and you can manually add nanoseconds if needed.
Example:
Output:
Conclusion
In Python, you can easily get the current time using multiple methods like ‘DateTime.now()’, the time() module, and ‘strftime()’. Each method offers flexibility to display time in various formats, such as UTC, GMT, and ISO. While `strftime()` is great for formatting time. Understanding these methods helps you retrieve the current time in Python.