Response: To retrieve the current time in Python, you may utilize the datetime.now() function.
Obtaining the present time is crucial for purposes such as documenting timestamps, logging events, or scheduling tasks within a program. This article will examine various techniques available for acquiring the current time in Python.
Python offers specific functions to output the current time. Let’s delve into these functions to retrieve the current time with illustrative examples:
Approach 1: Employing the datetime.now() Function in Python
The datetime.now() function facilitates printing the moment of program execution. It allows us to showcase the precise time at which the code was compiled.
Example:
Python
Code Copied!
Within this code, the format of the time displayed can be modified; for instance, if you wish to present the minutes first, adjust the format to “%M:%H:%S”. Similarly, you can vary the format of the time shown.
Output:
1. Utilizing attributes of the datetime.now() function
Several attributes such as day, month, year, minute, seconds, and hour can be displayed. Based on these attributes, here is an example:
Illustration:
Python
Code Duplicated!
Outcome:
2. Utilizing pytz along with datetime library in datetime.now() function
In the previous instance, we were only able to view the date, time, hour, and minutes, but it failed to indicate the location or the timezone. To display the timezone along with the date and time, we need a timezone-aware library named ‘pytz’.
"+data+"");
jQuery(".maineditor19740 .code-editor-output").show();
jQuery("#runBtn19740 i.run-code").hide();
}
})
}
function closeoutput19740() {
var code = editor19740.getSession().getValue();
jQuery(".maineditor19740 .code-editor-output").hide();
}
// Attach event listeners to the buttons
document.getElementById("copyBtn19740").addEventListener("click", copyCodeToClipboard19740);
document.getElementById("runBtn19740").addEventListener("click", runCode19740);
document.getElementById("closeoutputBtn19740").addEventListener("click", closeoutput19740);
Result:
3. Show UTC Date and Time utilizing utcnow() method
The acronym UTC represents Coordinated Universal Time; this UTC assists users worldwide in logging the user time.
Sample:
Python
Code Copied!
Result:
Method 2: Employing time() module to Show the Current Time in Python
To obtain the current time, we can utilize the time module in Python, and then the functions such as time.gmtime() or time.localtime() for retrieving time in UTC or local time, respectively.
Sample:
Python
Code Copied!
Result:
1. To Display Current Time In GMT Format
The abbreviation GMT stands for Greenwich Mean Time. To obtain the current time in GMT format using the time module, we utilize time.gmtime(), which returns the time in UTC.
Illustration:
Python
Code Successfully Copied!
In this illustration, you can observe that time.gmtime() returns the time in UTC, while time.strftime(‘%a, %d %b %Y %H:%M:%S GMT’, current_time_gmt) formats the time to appear as: Day, DD Month YYYY HH:MM:SS GMT.
Result:
``````html
2. To Present the Current Time in ISO Format:
Similarly to the prior example, we can derive an ISO format of the time using iso_format_time. Below is an illustration:
Illustration:
Python
Code Copied!
Within this illustration, the time has been transformed from GMT to ISO.
Result:
3. To Reveal milliseconds in time()
Now, let’s attempt to present the current time inclusive of milliseconds utilizing the time module. You can employ time.time() to retrieve the current time in seconds and subsequently format it to comprise milliseconds.
Illustration:
Python
Code Copied!
Output:
4. Displaying Nanoseconds in time()
To represent nanoseconds utilizing the time module, you can leverage time.time() for seconds; however, it does not directly yield nanoseconds. For accurate time tracking with nanosecond resolution, the time module provides time_ns(), which delivers the current time in nanoseconds.
Example:
Python
Code Copied!
Output:
Method 3: Utilizing strftime() method to Present the Current Time in Python
Although strftime() does not inherently accommodate nanoseconds, you can format the time using milliseconds from datetime, and it is possible to append nanoseconds manually if necessary.
Example:
Python
Code Copied!
Output:
Conclusion
In Python, obtaining the current time can be accomplished through various techniques such as ‘DateTime.now()’, the time() module, and ‘strftime()’. Each technique provides the versatility to present time in different formats, including UTC, GMT, and ISO. While `strftime()` is efficient for formatting time, gaining insights on these techniques can aid in retrieving the current time in Python.