ZetCode

Python time.strftime 函数

上次修改时间:2025 年 4 月 11 日

本综合指南探讨 Python 的 time.strftime 函数,该函数将时间元组格式化为可读的字符串。我们将介绍格式代码、常用模式和实际的格式化示例。

基本定义

time.strftime 函数根据格式规范将时间元组或 struct_time 转换为字符串。它是 strptime 的逆函数。

主要特点:具有区域设置感知的格式化,支持多种格式代码,并可与 time.struct_time 对象一起使用。 该函数根据格式规范返回一个字符串。

基本时间格式化

此示例演示了 strftime 的基本用法,用于将当前时间格式化为不同的字符串表示形式。

basic_formatting.py
import time

# Get current time as struct_time
current_time = time.localtime()

# Format using strftime
print(time.strftime("%Y-%m-%d", current_time))  # 2025-04-11
print(time.strftime("%A, %B %d %Y", current_time))  # Friday, April 11 2025
print(time.strftime("%I:%M:%S %p", current_time))  # 02:30:45 PM
print(time.strftime("%H:%M:%S", current_time))  # 14:30:45

这显示了常见的格式代码:%Y 表示年份,%m 表示月份,%d 表示日期,%H 表示 24 小时制,%I 表示 12 小时制,%M 表示分钟,%S 表示秒。

除非被覆盖,否则该函数使用当前的区域设置来确定月份/日期名称和 AM/PM 指示符。

自定义日期格式

strftime 允许通过组合格式代码来创建自定义日期格式。 此示例显示了各种日期格式化选项。

custom_formats.py
import time

now = time.localtime()

# Different date formats
print(time.strftime("%d/%m/%Y", now))  # 11/04/2025 (European style)
print(time.strftime("%m-%d-%y", now))  # 04-11-25 (US short style)
print(time.strftime("%Y%m%d", now))  # 20250411 (ISO compact)
print(time.strftime("%A %d %B, %Y", now))  # Friday 11 April, 2025
print(time.strftime("%Y-%j", now))  # 2025-101 (year and day of year)

%j 格式代码显示一年中的第几天 (1-366)。%A 和 %B 分别给出完整的星期几和月份名称。

不同的地区偏好不同的日期格式 - strftime 使您可以轻松适应本地约定。

时间格式化选项

此示例专门关注时间格式化,包括各种精度和表示选项。

time_formats.py
import time

now = time.localtime()

# Time formatting examples
print(time.strftime("%H:%M", now))  # 14:30 (24-hour with minutes)
print(time.strftime("%I:%M:%S %p", now))  # 02:30:45 PM (12-hour with AM/PM)
print(time.strftime("%H:%M:%S.%f", now))  # 14:30:45.123456 (with microseconds)
print(time.strftime("%X", now))  # 14:30:45 (locale's time representation)

请注意,由于 struct_time 不存储微秒,因此 %f 用于微秒可能无法在所有平台上运行。 %X 代码使用特定于区域设置的时间格式。

为了进行精确的时间测量,请考虑使用原生支持微秒的 datetime。

区域设置感知格式化

strftime 尊重区域设置中月份/日期名称和其他特定于区域设置的表示形式。 此示例演示了这一点。

locale_formatting.py
import time
import locale

now = time.localtime()

# Default locale
print(time.strftime("%A, %B %d", now))  # Friday, April 11

# Change to German locale
locale.setlocale(locale.LC_TIME, 'de_DE')
print(time.strftime("%A, %B %d", now))  # Freitag, April 11

# Change to French locale
locale.setlocale(locale.LC_TIME, 'fr_FR')
print(time.strftime("%A, %B %d", now))  # vendredi, avril 11

区域设置会影响星期几的名称 (%A)、月份名称 (%B)、AM/PM (%p) 和其他文本表示形式。

请记住在生产代码中处理潜在的区域设置可用性问题。

组合日期和时间

此示例显示了如何通过组合日期和时间格式代码来创建综合的日期时间字符串。

datetime_formatting.py
import time

now = time.localtime()

# Common combined formats
print(time.strftime("%c", now))  # Fri Apr 11 14:30:45 2025 (locale's default)
print(time.strftime("%Y-%m-%d %H:%M:%S", now))  # 2025-04-11 14:30:45
print(time.strftime("%a %b %d %H:%M:%S %Y", now))  # Fri Apr 11 14:30:45 2025
print(time.strftime("%A, %B %d %Y at %I:%M %p", now))  # Friday, April 11 2025 at 02:30 PM

%c 格式代码提供区域设置的相应日期和时间表示形式。 其他组合可以精确控制输出。

对于 ISO 8601 格式,请考虑改用 datetime.isoformat()。

特殊格式代码

strftime 包括几个特殊格式代码,用于不太常见但有用的表示形式。 此示例演示了它们。

special_codes.py
import time

now = time.localtime()

# Special format codes
print(time.strftime("%U", now))  # 15 (week number, Sunday first)
print(time.strftime("%W", now))  # 15 (week number, Monday first)
print(time.strftime("%w", now))  # 5 (weekday as decimal, 0=Sunday)
print(time.strftime("%j", now))  # 101 (day of year)
print(time.strftime("%Z", now))  # EDT (timezone name)
print(time.strftime("%z", now))  # -0400 (UTC offset)

周数计算(%U、%W)因区域设置而异。 时区信息(%Z、%z)取决于平台支持。

这些代码对于专门的日期计算和具有时区意识的应用程序非常有用。

格式化 UTC 时间

此示例显示了如何使用 time.gmtime 而不是 time.localtime 来格式化 UTC 时间而不是本地时间。

utc_formatting.py
import time

# Get UTC time
utc_time = time.gmtime()

# Format UTC time
print(time.strftime("%Y-%m-%d %H:%M:%S UTC", utc_time))  # 2025-04-11 18:30:45 UTC
print(time.strftime("%H:%M Z", utc_time))  # 18:30 Z (Zulu time)
print(time.strftime("%a, %d %b %Y %H:%M:%S GMT", utc_time))  # Fri, 11 Apr 2025 18:30:45 GMT

UTC 格式化对于需要与时区无关的时间戳的系统或对于网络协议至关重要。

'Z' 后缀表示 Zulu 时间 (UTC+0),这在航空和军事领域很常见。

最佳实践

资料来源

作者

我叫 Jan Bodnar,是一名充满热情的程序员,拥有丰富的编程经验。 我从 2007 年开始撰写编程文章。 迄今为止,我已经撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出所有 Python 教程