Python strftime()

Python strftime() function is present in datetime and time modules to create a string representation based on the specified format string.

datetime和time模块中提供了Python strftime()函数,用于基于指定的格式字符串创建字符串表示形式。

Python strftime() (Python strftime())

  • Python datetime module strftime() function and time module strftime() function behaves exactly the same. However, their syntax is slightly different.

    Python datetime module strftime() function syntax is:

    datetime_object.strftime(format_str)

    Python time module strftime() function syntax is:

    This function converts a tuple or struct_time object to a string as specified by the format argument. If ‘time_object’ is not provided, the current time as returned by localtime() is used. The format argument must be a string.

    Python datetime模块 strftime()函数的语法为:

    datetime_object.strftime(format_str)

    Python时间模块 strftime()函数的语法为:

    此函数将元组或struct_time对象转换为format参数指定的字符串。 如果未提供“ time_object”,则使用localtime()返回的当前时间。 format参数必须是字符串。

  • For time objects (datetime.time()), the format codes for the year, month, and day should not be used, as time objects have no such values. If they’re used anyway, 1900 is substituted for the year, and 1 for the month and day.

    对于时间对象(datetime.time()),不应使用年,月和日的格式代码,因为时间对象没有此类值。 如果仍然使用它们,则用1900代替年份,用1代替月份和日期。
  • For date objects (datetime.date()), the format codes for hours, minutes, seconds, and microseconds should not be used, as date objects have no such values. If they’re used anyway, 0 is substituted for them.

    对于日期对象(datetime.date()),不应使用小时,分钟,秒和微秒的格式代码,因为日期对象没有此类值。 如果仍然使用它们,则将0替换为它们。

Python strftime()格式指令 (Python strftime() format directives)

Following table list outs the most commonly used formatting directive that can be used in format string.

下表列出了可以在格式字符串中使用的最常用的格式指令。

DirectiveDescriptionExample Output
%aWeekday as locale’s abbreviated name.Sun, Mon, …, Sat (en_US)
So, Mo, …, Sa (de_DE)
%AWeekday as locale’s full name.Sunday, Monday, …, Saturday (en_US)
Sonntag, Montag, …, Samstag (de_DE)
%wWeekday as a decimal number, where 0 is Sunday and 6 is Saturday.0, 1, 2, 3, 4, 5, 6
%dDay of the month as a zero-padded decimal number.01, 02, …, 31
%bMonth as locale’s abbreviated name.Jan, Feb, …, Dec (en_US)
Jan, Feb, …, Dez (de_DE)
%BMonth as locale’s full name.January, February, …, December (en_US)
Januar, Februar, …, Dezember (de_DE)
%mMonth as a zero-padded decimal number.01, 02 … 12
%yYear without century as a zero-padded decimal number.01, 02, … 99
%YYear with century as a decimal number.0001, 0002, … , 9999
%HHour (24-hour clock) as a zero-padded decimal number.01, 02, … , 23
%IHour (12-hour clock) as a zero-padded decimal number.01, 02, … , 12
%pLocale’s equivalent of either AM or PM.AM, PM (en_US)
am, pm (de_DE)
%MMinute as a zero-padded decimal number.01, 02, … , 59
%SSecond as a zero-padded decimal number.01, 02, … , 59
%fMicrosecond as a decimal number, zero-padded on the left.000000, 000001, …, 999999
Not applicable with time module.
%zUTC offset in the form ±HHMM[SS] (empty string if the object is naive).(empty), +0000, -0400, +1030
%ZTime zone name (empty string if the object is naive).(empty), UTC, IST, CST
%jDay of the year as a zero-padded decimal number.001, 002, …, 366
%UWeek number of the year (Sunday as the first day of the week) as a zero padded decimal number.
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) as a decimal number.
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.Tue Aug 16 21:30:00 1988 (en_US)
Di 16 Aug 21:30:00 1988 (de_DE)
%xLocale’s appropriate date representation.08/16/88 (None)
08/16/1988 (en_US)
16.08.1988 (de_DE)
%XLocale’s appropriate time representation.21:30:00 (en_US)
21:30:00 (de_DE)
%%A literal ‘%’ character.%
指示 描述 示例输出
%一个 工作日为语言环境的缩写名称。 周日,周一,…,周六(en_US)
因此,Mo,...,Sa(de_DE)
%一个 工作日为语言环境的全名。 周日,周一,…,周六(zh_CN)
桑塔格,蒙塔格,…,萨姆斯塔格(de_DE)
%w 以十进制数表示的工作日,其中0是星期日,6是星期六。 0、1、2、3、4、5、6
%d 月份中的一天,以零填充的十进制数字表示。 01,02,…,31
%b 月作为语言环境的缩写名称。 一月,二月,…,十二月(en_US)
一月,二月,…,德兹(de_DE)
%B 月作为语言环境的全名。 一月,二月,…,十二月(zh_CN)
Januar,Februar,...,Dezember(de_DE)
%m 以零填充的十进制数字表示的月份。 01,02…12
%y 无世纪的年份,为零填充的十进制数字。 01,02,…99
%Y 以世纪作为十进制数字的年份。 0001、0002,…,9999
%H 小时(24小时制),为补零的十进制数字。 01,02,…,23
%一世 小时(12小时制),为零填充的十进制数字。 01,02,…,12
%p 相当于AM或PM的语言环境。 上午,下午(en_US)
上午,下午(de_DE)
%M 分钟,为零填充的十进制数字。 01,02,…,59
%S 第二个为零填充的十进制数。 01,02,…,59
%F 微秒,十进制数,在左侧补零。 000000,000001,…,999999
不适用于时间模块。
%z UTC偏移量,格式为±HHMM [SS](如果对象是天真对象,则为空字符串)。 (空),+ 0000,-0400,+ 1030
%Z 时区名称(如果对象是天真对象,则为空字符串)。 (空),UTC,IST,CST
%j 一年中的一天,为零填充的十进制数字。 001,002,…,366
%U 一年中的周号(星期日为一周的第一天),以零填充的十进制数表示。
新年中第一个星期日之前的所有天均视为第0周。
00,01,…,53
%W 一年中的星期数(星期一为一周的第一天),以十进制数表示。
第一个星期一之前的新的一年中的所有天均视为在第0周。
00,01,…,53
%C 语言环境的适当日期和时间表示。 星期二八月16 21:30:00 1988(zh_CN)
Di 16 Aug 21:30:00 1988(de_DE)
%X 语言环境的适当日期表示形式。 88年8月16日(无)
1988年8月16日(zh_CN)
1988年8月16日(de_DE)
%X 语言环境的适当时间表示形式。 21:30:00(zh_CN)
21:30:00(de_DE)
%% 文字“%”字符。

Python strftime()示例 (Python strftime() example)

日期时间模块 (datetime module)

Let’s look at some examples of using strftime() function with datetime module.

让我们看一些将strftime()函数与datetime模块一起使用的示例。

import datetime

dt = datetime.datetime.now()

print('DateTime in Default Formatting:', dt)

print('Formatted DateTime', dt.strftime("%m/%d/%y %H:%M:%S"))

print("Current year:", dt.strftime("%Y"))
print("Month of year:", dt.strftime("%B"))
print("Week number of the year:", dt.strftime("%W"))
print("Weekday of the week:", dt.strftime("%w"))
print("Day of year:", dt.strftime("%j"))
print("Day of the month:", dt.strftime("%d"))
print("Day of week:", dt.strftime("%A"))

Output:

输出:

DateTime in Default Formatting: 2018-09-17 12:10:48.081992
Formatted DateTime 09/17/18 12:10:48
Current year: 2018
Month of year: September
Week number of the year: 38
Weekday of the week: 1
Day of year: 260
Day of the month: 17
Day of week: Monday

时间模块 (time module)

import time
seconds = time.time()
t = time.localtime(seconds)

print('Time struct_time object:', t)
print('Time object with pre-defined formatting:', time.asctime(t))


print('Formatted Time:', time.strftime("%m/%d/%y %H:%M:%S", t))

print("Current year:", time.strftime("%Y", t))
print("Month of year:", time.strftime("%B", t))
print("Week number of the year:", time.strftime("%W", t))
print("Weekday of the week:", time.strftime("%w", t))
print("Day of year:", time.strftime("%j", t))
print("Day of the month:", time.strftime("%d", t))
print("Day of week:", time.strftime("%A", t))

时区 (TimeZone)

We know that the default datetime object doesn’t have timezone information, let’s see what is the output if we try to print its timezone.

我们知道默认的datetime对象没有时区信息,让我们看看尝试打印其时区的输出是什么。

print("TimeZone:", dt.strftime("%Z"))

Output: TimeZone:

输出:时TimeZone:

Let’s see the output with time module object.

让我们看一下带有时间模块对象的输出。

print("TimeZone:", time.strftime("%Z", t))

Output: TimeZone: IST

输出:时TimeZone: IST

We can use pytz module to create a timezone aware datetime object.

我们可以使用pytz模块创建一个时区感知的datetime对象。

import pytz
utc = pytz.utc
dt = datetime.datetime.now(utc)
print("TimeZone:", dt.strftime("%Z"))

Output: TimeZone: UTC

输出:时TimeZone: UTC

GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。

References: datetime strftime(), time strftime()

参考: datetime strftime()时间strftime()

翻译自: https://www.journaldev.com/23324/python-strftime

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值