带有示例的Python strftime()函数格式指令

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()函数具有非常大的格式代码/指令,用作格式参数。 下面列出了所有这些格式,并且所有这些格式代码/指令均在双引号内以字符串形式提供。

DirectiveMeaningExample
%aAbbreviated weekday name.Sun, Mon, …
%AFull weekday name.Sunday, Monday, …
%wWeekday as a decimal number.0, 1, …, 6
%dDay of the month as a zero-padded decimal.01, 02, …, 31
%-dDay of the month as a decimal number.1, 2, …, 30
%bAbbreviated month name.Jan, Feb, …, Dec
%BFull month name.January, February, …
%mMonth as a zero-padded decimal number.01, 02, …, 12
%-mMonth as a decimal number.1, 2, …, 12
%yYear without century as a zero-padded decimal number.00, 01, …, 99
%-yYear without century as a decimal number.0, 1, …, 99
%YYear with century as a decimal number.2013, 2019 etc.
%HHour (24-hour clock) as a zero-padded decimal number.00, 01, …, 23
%-HHour (24-hour clock) as a decimal number.0, 1, …, 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, …, 12
%-IHour (12-hour clock) as a decimal number.1, 2, … 12
%pLocale’s AM or PM.AM, PM
%MMinute as a zero-padded decimal number.00, 01, …, 59
%-MMinute as a decimal number.0, 1, …, 59
%SSecond as a zero-padded decimal number.00, 01, …, 59
%-SSecond as a decimal number.0, 1, …, 59
%fMicrosecond as a decimal number, zero-padded on the left.000000 – 999999
%zUTC offset in the form +HHMM or -HHMM.
%ZTime zone name.
%jDay of the year as a zero-padded decimal number.001, 002, …, 366
%-jDay of the year as a decimal number.1, 2, …, 366
%UWeek 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
%WWeek 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
%cLocale’s appropriate date and time representation.Mon Sep 30 07:06:05 2013
%xLocale’s appropriate date representation.09/30/13
%XLocale’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
%% 文字'%'字符。
LEARN MORE  Python Date Time Operations Tutorial with Examples
了解更多Python日期时间操作教程及示例
strftime() Function Format
strftime() Function Format
strftime()函数格式
strftime() Function Format

strftime() Function Format

strftime()函数格式

strftime() Function Format
strftime() Function Format
strftime()函数格式
strftime() Function Format
strftime() Function Format
strftime()函数格式

打印当前日期和时间(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 %Yformat 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格式说明符来打印微秒信息。 此信息将是十进制数,并在左侧补零。

LEARN MORE  How To Use Python sleep() Function?
了解更多信息如何使用Python sleep()函数?

印刷周日 (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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值