The strftime() function in PythonIn this tutorial, we will understand the working of the strftime() function in the Python programming language along with its variations. So, let us begin. As we already know, there are a variety of modules provided by Python that contain clusters of functions in order to implement different types of functionalities on the data. The time module of Python is utilized to implement manipulations based on the various timestamp all over. Moreover, the strftime() function of Python accepts the time in different formats and returns a string with the time representing in the standard form. Using Python strftime() function to get the present timeWe can use the Python strftime() function together with the datetime module in order to fetch the present timestamp in a proper format as per the format codes. The syntax for the same is shown below. Syntax: Here, the parameter of the strftime() function, format codes, are actually the predefined codes utilized in order to represent the timestamp in a standard and proper way. We will understand much more about the format codes in detail throughout this tutorial. But before that, let us consider an example. Example: Output: Present Time: 18 : 06 : 17 Present Date: 01 - 06 - 2021 Explanation: In the above example, we have imported the datetime module from the datetime library. We have then defined a variable storing the value of the current date and time. We have then used the strftime() function to represent the current date and time into the specified standard format and printed them to the users. As a result, the current date and time have been printed successfully in the proper and standard format. Using Python strftime() function along with a predefined timestampSometimes it happens when we want to show the date and time of the past datasets. We can perform the same with the help of the strftime() function in Python. The fromtimestamp() method of the datetime module helps the user to fetch the predefined timestamp. Moreover, we can use the strftime() function in order to represent that fetched timestamp in a standard format using the different format codes as explained earlier. We can see the syntax for the usage of the strftime() function as follows: Syntax: Now, let us consider an example illustrating the working of the strftime() function together with a predefined timestamp. Example: Output: Time as per the provided Timestamp: 09 : 15 : 23 Date as per the provided Timestamp: 19 - 08 - 1973 Explanation: In the above example, we have then defined the variable storing the value of the predefined timestamp. We have then used the fromtimestamp() method to fetch the stored value and the strftime() function to represent the fetched timestamp into a proper and standard format. We have then printed the resultant values for the users. As a result, the timestamp has been represented in the proper specified format successfully. Using various Format codes with Python strftime() functionPython strftime() function utilizes different format codes in order to represent date and time in a maintained and standard form. Moreover, these format codes can be used to segregate hours, days, weeks, and a lot more from the timestamp and show the same. Let us consider an example representing the usage of Format code. Example: Output: Present Day: Tuesday Explanation: In the above example, we have imported the strftime function from the time module. We have then defined a variable that uses the strftime() function along with the parameter “%A” as the format code for the current day of the local time. We have then printed the values for the user. As a result, the program has printed the full name of the day accordingly. Now let us consider some more examples using other Format codes. 1. Using “%c” as a format code in order to show the current local time in the following format: Day Month Date hours:minutes:seconds Year Example: Output: Present Timestamp: Wed Jun 2 16:06:41 2021 Explanation: In the above program, we have again carried out the equivalent procedure we did earlier. However, we have used “%c” as the format code. As a result, the program has printed the current timestamp in the format code specified above. 2. Using “%R” as a format code in order to represent the time in a 24-hour format. Example: Output: Present Time in 24-hour format: 16:14 Explanation: We have again followed the same syntax in the above program, but we include the format code “%R” to display the current time in 24-hour format. 3. Using “%r” as a format code in order to represent the time in 12-hour format or Hours:Minutes:Seconds format along with the AM or PM notation. Example: Output: Present Time in 12-hour format: 04:33:12 PM Explanation: Similar to what we have done earlier, we have added the “%r” as the parameter for the strftime() function to print the current time in 12-hour format. 4. Using multiple format codes in a single function. Example: Output: Present Timestamp: 06/02/21 -- 16:44:11 PM Explanation: In the above example, we have used the format code “%x” in order to show the date from the local timestamp. We have also utilized the format code “%X” in order to display the current time in 24-hour format along with the format code “%p” in order to provide the notation for the current time, i.e., AM or PM. Apart from the above format codes, there are many format codes available. These format codes are listed below:
|
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263500.html