Explain how to use strftime () in Python
This article describes how to use the strftime () method in Python. It is the basic knowledge of getting started with Python. For more information, see
The strftime () method is converted into a tuples or struct_time, which indicates that the gmtime () or localtime () returned by the format parameter specified by the time is a string.
If t is not set, the current time returned uses the localtime () method. The format must be a string. Exception ValueError is suspended if t is out of the permitted range of any field.
Syntax
The syntax of the strftime () method is as follows:
?
1 |
Time. strftime (format [, t]) |
Parameters
T -- this is the time in seconds for formatting.
Format -- this is the command that will be used to format a given time. The following command can embed a format string:
Command
% A-abbreviated day of the week
% A-complete day of the week
% B-abbreviated month name
% B-complete month name
% C-expression of the preferred date and time
% C-century value (year divided by 100, range: 00 to 99)
% D-the day of the month (01? 31)
% D-similar to % m/% d/% y
% E-the day of the month (1? 31)
% G-similar to % G, but not Century
% G-year corresponding to the four-digit ISO week (see % V)
% H-similar to % B
% H-hour, in 24-hour format (00? 23)
% I-hour, in 12-hour format (01? 12)
% J-which day of the year (001? 366)
% M-month (01? 12)
% M-min
% N-linefeed
% P-according to the given time value am or pm
% R-symbol of the time in the morning and afternoon: am/pm
% R-time in 24 hour notation
% S-second
% T-Tab
% T-current time, equal to % H: % M: % S
% U-workday is a number (1 to 7), Monday = 1. Warning Sunday = 1 on Sun Solaris
% U-the number of weeks in the current year. The first Sunday is the first day of the first week.
% V-the number of weeks of ISO 8601 of the current year (01 to 53), of which week 1st is the first week of at least four days of the current year, and Monday is the first day of the week.
% W-the week number of the current year, and the first Monday as the first day of the first week
% W-week is a decimal number, Sunday = 0
% X-date representation without time
% X-time representation with no date preference
% Y-century-free year (range: 00 to 99)
% Y-this year, including Century
% Z or % z-time zone or name or abbreviation
%-Text % characters
Return Value
This method does not return any value.
Example
The following example shows how to use the strftime () method.
?
1 2 3 4 5 6 |
#! /Usr/bin/python Import time T = (2009, 2, 17, 17, 3, 38, 1, 48, 0) T = time. mktime (t) Print time. strftime ("% B % d % Y % H: % M: % S", time. gmtime (t )) |
When we run the above program, it will produce the following results: