Python strftime()

时间:2020-02-23 14:43:22  来源:igfitidea点击:

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

Python strftime()

  • Python datetime模块strftime()函数和time模块strftime()函数的行为完全相同。
    但是,它们的语法略有不同.Python datetime模块strftime()函数的语法为:
    Python时间模块strftime()函数的语法为:
    此函数将元组或者struct_time对象转换为format参数指定的字符串。
    如果未提供" time_object",则使用localtime()返回的当前时间。
    format参数必须是字符串。

  • 对于时间对象(datetime.time()),不应使用年,月和日的格式代码,因为时间对象没有此类值。
    如果仍然使用它们,则用1900代替年份,用1代替月份和日期。

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

Python strftime()格式指令

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

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, …, 999999Not 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.%

Python strftime()示例

日期时间模块

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

datetime_object.strftime(format_str)

输出:

time.strftime(format_str[, time_object])

时间模块

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"))

时区

我们知道默认的datetime对象没有时区信息,因此,如果尝试打印其时区,我们将看到什么输出。

DateTime in Default Formatting: 2016-09-17 12:10:48.081992
Formatted DateTime 09/17/18 12:10:48
Current year: 2016
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

输出:TimeZone:

我们来看看带有时间模块对象的输出。

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))

输出:"时区:IST"

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

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

输出:"时区:UTC"