Python provides the strftime()
function in order to print the current of a given date in different formats. The full name of the strftime()
is string format time which will print the Python date and time structure in string format according to the given format.
Python提供了strftime()
函数,以便以不同的格式打印给定日期的当前值。 strftime()
的全名是字符串格式的时间,它将根据给定的格式以字符串格式打印Python日期和时间结构。
strftime()函数语法 (strftime() Function Syntax)
The strftime() function is provided by the time
library or module. So in order to use strftime() function, the time
library should be imported. strftime() function do not return any value.
time
库或模块提供了strftime()函数。 因此,为了使用strftime()函数,应导入time
库。 strftime()函数不返回任何值。
strftime(FORMAT)
- `FORMAT` will set the format of the date and time string. There are a lot of different format options which are explained below in the `Format Code/Directive List` part. “ FORMAT”将设置日期和时间字符串的格式。 有很多不同的格式选项,下面在“格式代码/指令列表”部分中进行了说明。
strftime()函数格式代码/指令列表 (strftime() Function Format Code/Directives List)
strftime() function has very large format code/directives which are used as the format parameter. Below all of them are listed and all these format code/directives are provided as a string inside double-quotes.
strftime()函数具有非常大的格式代码/指令,用作格式参数。 下面列出了所有这些格式,并且所有这些格式代码/指令均在双引号内以字符串形式提供。
Directive | Meaning | Example |
%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, …, 99 |
%Y | Year with century as a decimal number. | 2013, 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, …, 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 | Locale’s 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 |
%U | Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. | 00, 01, …, 53 |
%W | Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. | 00, 01, …, 53 |
%c | Locale’s appropriate date and time representation. | Mon Sep 30 07:06:05 2013 |
%x | Locale’s appropriate date representation. | 09/30/13 |
%X | Locale’s appropriate time representation. | 07:06:05 |
%% | A literal ‘%’ character. | % |
指示 | 含义 | 例 |
%一种 | 工作日名称的缩写。 | 周日,周一... |
%一种 | 工作日全名。 | 星期天星期一, … |
%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,…,99 |
%Y | 以世纪作为十进制数字的年份。 | 2013、2019等 |
%H | 小时(24小时制),为零填充的十进制数字。 | 00,01,…,23 |
%-H | 小时(24小时制)为十进制数字。 | 0,1,…,23 |
%一世 | 小时(12小时制),为零填充的十进制数字。 | 01,02,…,12 |
%-一世 | 小时(12小时制)为十进制数字。 | 1、2、12 |
%p | 语言环境的上午或下午。 | 上午下午 |
%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 |
%U | 一年中的第几周(星期日为一周的第一天)。 新年中第一个星期日之前的所有天均视为第0周。 | 00,01,…,53 |
%W | 一年中的第几周(星期一为一周的第一天)。 第一个星期一之前的新的一年中的所有天均视为在第0周。 | 00,01,…,53 |
%C | 语言环境的适当日期和时间表示。 | 2013年9月30日星期一07:06:05 |
%X | 语言环境的适当日期表示形式。 | 13/9/30 |
%X | 语言环境的适当时间表示形式。 | 07:06:05 |
%% | 文字'%'字符。 | % |
![strftime() Function Format](https://i-blog.csdnimg.cn/blog_migrate/8330a24569a980fa0ef40ebb27c8f5da.png)
![strftime() Function Format](https://i-blog.csdnimg.cn/blog_migrate/3df7ec8b12f5f764006457363b4cbfd6.png)
strftime() Function Format
strftime()函数格式
![strftime() Function Format](https://i-blog.csdnimg.cn/blog_migrate/96701a89a64ba7e7a9f4578c9f1602b1.png)
![strftime() Function Format](https://i-blog.csdnimg.cn/blog_migrate/d7a910d50cd7e3296ff981232c727c86.png)
打印当前日期和时间(Print Current Date and Time)
We will start with a simple example where we will print the date and time by using strftime() function. We will provide "%m/%d/%Y, %H:%M:%S"
format specifier which provides month, day, year, hour, minute, second information.
我们将从一个简单的示例开始,在该示例中,我们将使用strftime()函数打印日期和时间。 我们将提供"%m/%d/%Y, %H:%M:%S"
格式说明符,该说明符提供月,日,年,时,分,秒信息。
d = time.strftime("%m/%d/%Y, %H:%M:%S")
print(d)
#Will output following line
# 12/23/2019, 20:34:04
以mm-dd-yyyy格式打印当前日期 (Print Current Date in mm-dd-yyyy Format)
We can also print the current date in mm-dd-yyyy
format by using the "%m-%d-%Y"
format specifier.
我们还可以使用"%m-%d-%Y"
格式说明符以mm-dd-yyyy
格式打印当前日期。
d = time.strftime("%m-%d-%Y")
print(d)
# Will output 12-23-2019
印刷年 (Print Year)
The current year can be expressed with the %Y
format directive. If we want to print only the last two digits of the year we can use %y
format directive.
当前年份可以用%Y
格式指令表示。 如果只想打印年份的最后两位数字,则可以使用%y
format指令。
d = time.strftime("%Y")
print(d)
# Output will be 2019
d = time.strftime("%y")
print(d)
#Output will be 19
列印月份 (Print Month)
We can print the month number like 5,7,9 by using %m
format directive but if we need the month name we can use %B
for full month name and %b
for abbreviated month name.
我们可以使用%m
format指令打印类似5、7、9的月份号,但是如果需要月份名称,可以将%B
用作完整的月份名称,将%b
用作缩写的月份名称。
d = time.strftime("%m")
print(d)
#Output will be 12
d = time.strftime("%B")
print(d)
#Output will be December
d = time.strftime("%b")
print(d)
#Output will be Dec
印刷日 (Print Day)
We can print the current day number by using the %d
. We can also print the full weekday name with %A
and abbreviated weekday name with %a
.
我们可以使用%d
打印当前日期。 我们还可以使用%A
打印完整的工作日名称,并使用%a
打印缩写的工作日名称。
d = time.strftime("%d")
print(d)
#Output will be 23
d = time.strftime("%A")
print(d)
#Output will be Monday
d = time.strftime("%a")
print(d)
#Output will be Mon
印刷时间 (Print Hour)
We can print the hour information by using the %H
format specifier which will present hour in 24-hour clock format. %-H
can be used present hour in 24-hour clock format as a decimal number. %I
is used to present in 12-hour format.
我们可以使用%H
格式说明符来打印小时信息,该说明符将以24小时制显示小时。 %-H
可以以24小时制作为十进制数字在当前时间使用。 %I
用于以12小时格式显示。
d = time.strftime("%H")
print(d)
#The output will be 21
d = time.strftime("%-H")
print(d)
#The output will be 21
d = time.strftime("%I")
print(d)
#The output will be 09
打印分钟 (Print Minute)
We can print the minute information by using the %M
for a zero-padded decimal number or %-M
for decimal number.
我们可以通过将%M
用作填充零的十进制数字或将%-M
用作十进制数字来打印分钟信息。
d = time.strftime("%M")
print(d)
#The output will be 22
d = time.strftime("%-M")
print(d)
#The output will be 22
列印第二 (Print Second)
We can print the second information by using the %S
for zero-padded decimal number and %-S
as a decimal number.
我们可以通过使用%S
代表零填充的十进制数字和%-S
作为十进制数字来打印第二个信息。
d = time.strftime("%S")
print(d)
#The output will be 47
d = time.strftime("%-S")
print(d)
#The output will be 53
打印微秒 (Print Microsecond)
Microsecond information can be printed by using a %f
format specifier. This information will be decimal number and zero-padded on the left.
可以使用%f
格式说明符来打印微秒信息。 此信息将是十进制数,并在左侧补零。
印刷周日 (Print Week Day)
We can also print a weekday number as a decimal with a %w
format specifier. The name of the day can be printed with %A
as full or %a
as an abbreviated weekday name.
我们还可以使用%w
格式说明符将工作日数字打印为小数。 可以使用%A
完整名称或%a
作为缩写的工作日名称打印日期名称。
d = time.strftime("%w")
print(d)
#The output will be 1
d = time.strftime("%A")
print(d)
#The output will be Monday
d = time.strftime("%a")
print(d)
#The output will be Mon
将月份打印为名称 (Print Month As Name)
We can also print the month name by using %B
which will print the full month name. The abbreviated month name can be printed with the %b
format directive. The month number can be printed with the %m
format specifier.
我们还可以使用%B
打印月份名称,该月份将打印完整的月份名称。 缩写的月份名称可以使用%b
format指令打印。 可以使用%m
格式说明符打印月份号。
d = time.strftime("%B")
print(d)
#The output will be December
d = time.strftime("%b")
print(d)
#The output will be Dec
d = time.strftime("%m")
print(d)
#The output will be 12
打印日期作为名称 (Print Day As Name)
We can also print the day name in full format by using %A
and abbreviated by using %a
format directives.
我们还可以使用%A
以全格式打印日期名称,并使用%a
格式指令%A
其缩写。
d = time.strftime("%A")
print(d)
# The output will be Monday
d = time.strftime("%a")
print(d)
# The output will be Mon
打印世纪 (Print The Century)
We can print the year information with the century by using %Y
.
我们可以使用%Y
打印带有世纪的年份信息。
打印上午或下午 (Print AM or PM)
We can print the AM or PM information by using %p
information. This can be especially useful while printing the hour in a 12-hour format.
我们可以使用%p
信息来打印AM或PM信息。 当以12小时格式打印小时时,此功能特别有用。
d = time.strftime("%p")
print(d)
#The output will be PM
打印当前时区 (Print Current TimeZone)
We can print the time zone information about the system by providing the %Z
option. Also, %z
can be used to print UTC offset in the form of +HHMM or -HHMM.
我们可以通过提供%Z
选项来打印有关系统的时区信息。 此外, %z
可以用于以+ HHMM或-HHMM的形式打印UTC偏移量。
d = time.strftime("%Z")
print(d)
#The output will be PST
d = time.strftime("%z")
print(d)
#The output will be -0800
翻译自: https://www.poftut.com/python-strftime-function-format-directives-with-example/