带有示例的Python date strftime()方法
Python date.strftime()方法 (Python date.strftime() Method)
date.strftime() method is used to manipulate objects of date class of module datetime.
date.strftime()方法用于操作模塊datetime的日期類的對象。
It takes an instance of the class and returns a string representing the date, which is controlled by an explicit format string. Since this class has attributes related to dates only, format codes referring to hours, minutes, or seconds have 0 values.
它使用類的實例,并返回表示日期的字符串,該字符串由顯式格式字符串控制。 由于此類僅具有與日期有關的屬性,因此引用小時,分鐘或秒的格式代碼具有0值。
Module:
模塊:
import datetimeClass:
類:
from datetime import dateSyntax:
句法:
strftime(format)Parameter(s):
參數:
- format - it is the string format code based on which string representation and formatting occurs. For example, %Y, %m, %d etc. are format codes. This method takes one or more format codes as an argument and returns a formatted string based on that. - format-它是字符串格式代碼,基于該代碼的字符串表示形式和格式發生。 例如,%Y,%m,%d等是格式代碼。 此方法將一個或多個格式代碼作為參數,并根據該格式返回格式化的字符串。 
Given below is a list of all the format codes available:
以下是所有可用格式代碼的列表:
| %a | Abbreviated weekday name. | Sun, Mon, ... | 
| %A | Full weekday name. | Sunday, Monday, ... | 
| %w | Weekday as a decimal number. | 0, 1, ..., 6 | 
| %d | Day of the month as a zero-padded decimal. | 01, 02, ..., 31 | 
| %-d | Day of the month as a decimal number. | 1, 2, ..., 30 | 
| %b | Abbreviated month name. | Jan, Feb, ..., Dec | 
| %B | Full month name. | January, February, ... | 
| %m | Month as a zero-padded decimal number. | 01, 02, ..., 12 | 
| %-m | Month as a decimal number. | 1, 2, ..., 12 | 
| %y | Year without century as a zero-padded decimal number. | 00, 01, ..., 99 | 
| %-y | Year without century as a decimal number. | 0,1, 2, … , 99 | 
| %Y | Year with century as a decimal number. | 2020, 2019 etc | 
| %H | Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 | 
| %-H | Hour (24-hour clock) as a decimal number. | 0, 1, 2, ..., 23 | 
| %I | Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 | 
| %-I | Hour (12-hour clock) as a decimal number. | 1,2,.., 12 | 
| %p | Local AM or PM. | AM, PM | 
| %M | Minute as a zero-padded decimal number. | 00, 01, ..., 59 | 
| %-M | Minute as a decimal number. | 0, 1, ..., 59 | 
| %S | Second as a zero-padded decimal number. | 00, 01, ..., 59 | 
| %-S | Second as a decimal number. | 0, 1, ..., 59 | 
| %f | Microsecond as a decimal number,zero-padded on the left. | 000000 - 999999 | 
| %z | UTC offset in the form +HHMM or -HHMM. | |
| %Z | Time zone name. | |
| %j | Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 | 
| %-j | Day of the year as a decimal number. | 1, 2, ..., 366 | 
| %c | Appropriate local date and time representation | Wed Oct 27 01:04:15 2020 | 
| %x | Appropriate local date representation. | 09/30/13 | 
| %X | Appropriate local time representation. | 09:07:06 | 
| %U | Week number of the year, with Sunday as the first day of the week | 00, 01, ..., 53 | 
| %W | Week number of the year, with Monday as the first day of the week | 00, 01, ..., 53 | 
| %一個 | 工作日名稱的縮寫。 | 周日,周一... | 
| %一個 | 工作日全名。 | 星期天星期一, ... | 
| %w | 工作日為十進制數字。 | 0,1,...,6 | 
| %d | 月份中的一天,以零填充的十進制表示。 | 01,02,...,31 | 
| %d | 以十進制數表示的月份中的一天。 | 1,2,...,30 | 
| %b | 縮寫的月份名稱。 | 一月,二月,...,十二月 | 
| %B | 完整的月份名稱。 | 一月二月, ... | 
| %m | 以零填充的十進制數字表示的月份。 | 01、02,...,12 | 
| %-m | 以十進制數表示的月份。 | 1,2,...,12 | 
| %y | 無世紀的年份,為零填充的十進制數字。 | 00、01,...,99 | 
| %-y | 沒有世紀的年份作為十進制數字。 | 0,1,2,…,99 | 
| %Y | 以世紀作為十進制數字的年份。 | 2020、2019等 | 
| %H | 小時(24小時制),為補零的十進制數字。 | 00、01,...,23 | 
| %-H | 小時(24小時制)為十進制數字。 | 0,1,2,...,23 | 
| %一世 | 小時(12小時制),為零填充的十進制數字。 | 01、02,...,12 | 
| %-一世 | 小時(12小時制)為十進制數字。 | 1,2,..,12 | 
| %p | 本地AM或PM。 | 上午下午 | 
| %M | 分鐘,為零填充的十進制數字。 | 00、01,...,59 | 
| %-M | 以十進制數字表示。 | 0,1,...,59 | 
| %S | 第二個為零填充的十進制數。 | 00、01,...,59 | 
| %-S | 第二個十進制數字。 | 0,1,...,59 | 
| %F | 微秒,十進制數,在左側補零。 | 000000-999999 | 
| %z | UTC偏移量,格式為+ HHMM或-HHMM。 | |
| %Z | 時區名稱。 | |
| %j | 一年中的一天,為零填充的十進制數字。 | 001,002,...,366 | 
| %-j | 一年中的天,以十進制數字表示。 | 1,2,...,366 | 
| %C | 適當的本地日期和時間表示 | 2020年10月27日星期三01:04:15 | 
| %X | 適當的本地日期表示形式。 | 13/9/30 | 
| %X | 適當的本地時間表示。 | 09:07:06 | 
| %U | 一年中的第幾周,以星期日為一周的第一天 | 00、01,...,53 | 
| %W | 一年中的第幾周,星期一為一周的第一天 | 00、01,...,53 | 
Please note all the codes which have time and timezone information, will have 0 values as date class only contains date attributes.
請注意,所有具有時間和時區信息的代碼都將具有0值,因為日期類僅包含日期屬性。
Return value:
返回值:
The return type of this method is a string converted according to the format code.
此方法的返回類型是根據格式代碼轉換的字符串。
Example:
例:
## Python program explaining the ## use of strftime() method in date classfrom datetime import date## Creating an instance x = date.today() c = x.strftime("%c") print("Date representation using strftime:", c) print()##You can extract information using these methods print("Abbreviated weekday name:", x.strftime("%a")) print("Full weekday name:", x.strftime("%A")) print("Weekday as a decimal number:", x.strftime("%w")) print("Day of the month as a zero-padded decimal:", x.strftime("%d")) print("Full month name:", x.strftime("%B")) print("Month as a decimal number:", x.strftime("%-m")) print("Year with century as a decimal number:", x.strftime("%Y")) print("What day of the year is it?", x.strftime("%-j")) print("What is the week number of this day?", x.strftime("%U")) ##or x.strftime("%W") print()## Different ways of representing the Date ## as a date string print("Output 1:", x.strftime("%d %m %Y")) print("Output 2:", x.strftime("%-d %-m %y, %H %M %S")) ## time values will be 0 print("Output 3:", x.strftime("%a %d %B %Y")) print("Output 4:", x.strftime("%A, %Y %m %d, %H:%M:%S")) print("Output 5:", x.strftime("%x")) ## You can create any combination of the date ## representation you wantOutput
輸出量
Date representation using strftime: Thu Apr 30 00:00:00 2020Abbreviated weekday name: Thu Full weekday name: Thursday Weekday as a decimal number: 4 Day of the month as a zero-padded decimal: 30 Full month name: April Month as a decimal number: 4 Year with century as a decimal number: 2020 What day of the year is it? 121 What is the week number of this day? 17Output 1: 30 04 2020 Output 2: 30 4 20, 00 00 00 Output 3: Thu 30 April 2020 Output 4: Thursday, 2020 04 30, 00:00:00 Output 5: 04/30/20翻譯自: https://www.includehelp.com/python/date-strftime-method-with-example.aspx
總結
以上是生活随笔為你收集整理的带有示例的Python date strftime()方法的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: c#byte字节流的读取_C#中的byt
- 下一篇: bca ac如何联合索引_BCA的完整形
