Python时间模块格式代码指南
一、核心格式代码表
格式代码 | 描述 | 示例输出 |
%Y | 四位数年份 | 2024 |
%y | 两位数年份(00-99) | 24 |
%m | 补零月份(01-12) | 06 |
%d | 补零日期(01-31) | 01 |
%H | 24小时制补零小时(00-23) | 14 |
%I | 12小时制补零小时(01-12) | 02 |
%M | 补零分钟(00-59) | 05 |
%S | 补零秒(00-59) | 09 |
%f | 微秒(000000-999999) | 123456 |
%z | UTC偏移(±HHMM[SS]) | +0800 或 +08:00* |
%Z | 时区名称 | CST |
%A | 星期全名 | Monday |
%a | 星期缩写 | Mon |
%B | 月份全名 | June |
%b | 月份缩写 | Jun |
%j | 年中的第几天(001-366) | 123 |
%p | AM/PM(大写) | AM 或 PM |
%Z | 时区名称 | CST |
%z | UTC偏移(±HHMM) | +0800 |
%U | 年中的周数(周日为一周开始) | 22 |
%W | 年中的周数(周一为一周开始) | 22 |
%w | 星期几(0=周日) | 1 (周一) |
%c | 本地日期时间表示 | Mon Jun 1 14:05:09 2024 |
%C | 本世纪 | 20 |
%x | 本地日期表示 | 06/01/24 |
%X | 本地时间表示 | 14:05:09 |
二、代码示例
1. strftime 格式化(时间 → 字符串)
from datetime import datetime
now = datetime.now()
formatted = now.strftime("%Y-%m-%d %H:%M:%S.%f")
print(formatted) # 输出:2024-06-01 14:05:09.123456
weekday = now.strftime("今天是%A,本月是%B")
print(weekday) # 输出:今天是Saturday,本月是June
2. strptime 解析(字符串 → 时间)
date_str = "2024年06月01日 14:30"
dt = datetime.strptime(date_str, "%Y年%m月%d日 %H:%M")
print(dt) # 输出:2024-06-01 14:30:00
iso_str = "2024-06-01T14:30:45+08:00"
dt_utc = datetime.strptime(iso_str, "%Y-%m-%dT%H:%M:%S%z")
print(dt_utc) # 输出:2024-06-01 14:30:45+08:00
三、特殊场景处理
1.格式化日期时间
from datetime import datetime
now = datetime.now()
# 完整日期时间
print(now.strftime("%Y-%m-%d %H:%M:%S")) # 2023-01-01 12:00:00
# 带星期和月份名称
print(now.strftime("%A, %B %d, %Y")) # Monday, January 01, 2023
# 带AM/PM
print(now.strftime("%I:%M %p")) # 12:00 PM
# 带时区
print(now.strftime("%Y-%m-%d %H:%M:%S %Z")) # 2023-01-01 12:00:00 UTC
2.解析日期时间
from datetime import datetime
# 从字符串解析日期时间
dt_str = "2023-01-01 12:00:00"
dt = datetime.strptime(dt_str, "%Y-%m-%d %H:%M:%S")
print(dt) # 2023-01-01 12:00:00
# 解析带星期和月份名称的字符串
dt_str = "Monday, January 01, 2023"
dt = datetime.strptime(dt_str, "%A, %B %d, %Y")
print(dt) # 2023-01-01 00:00:00
3.毫秒/微秒处理
# 包含微秒的格式
micro_str = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
print(micro_str) # 输出:20240601_140530_123456
# 仅保留毫秒
millis_str = datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]
print(millis_str) # 输出:2024-06-01 14:05:30.123
4. 时区格式化
from datetime import datetime, timezone
# 带时区的时间
utc_time = datetime.now(timezone.utc)
print(utc_time.strftime("%Y-%m-%d %H:%M:%S %Z")) # 输出:2024-06-01 06:05:30 UTC
# 转换时区显示
bj_time = utc_time.astimezone(timezone(timedelta(hours=8)))
print(bj_time.strftime("%Y-%m-%d %H:%M:%S %z")) # 输出:2024-06-01 14:05:30 +0800
四、注意事项
- 平台差异:
%Z 在 Windows 上可能返回空字符串%c 在不同地区的输出格式不同
- 错误处理:
try:
datetime.strptime("2024/13/01", "%Y/%m/%d")
except ValueError as e:
print(f"解析错误: {e}") # 输出:month must be in 1..12
- 性能优化:
# 预编译格式(高频调用场景)
from datetime import datetime
ISO_FORMAT = "%Y-%m-%dT%H:%M:%S%z"
dt = datetime.strptime("2024-06-01T14:30:45+0800", ISO_FORMAT)
- ISO 8601格式
from datetime import datetime
dt = datetime(2024, 7, 9, 15, 30, 45, 123456)
print(dt.isoformat()) # 输出:2024-07-09T15:30:45.123456
# 对应格式化字符串
fmt = "%Y-%m-%dT%H:%M:%S.%f"
- 带时区解析(Python 3.7+)
# 解析带冒号的时区偏移
dt_str = "2024-01-23 15:30+08:00"
dt = datetime.strptime(dt_str, "%Y-%m-%d %H:%M%z")
五、常用格式模板
场景 | 格式字符串 | 示例输出 |
日志文件名 | "%Y%m%d_%H%M%S" | 20240601_143045 |
文件命名 | %Y%m%d_%H%M%S | 20240123_153045 |
中文日期 | %Y年%m月%d日 | 2024年01月23日 |
12小时制时间 | %I:%M:%S %p | 03:30:45 PM |
周数显示 | 第%W周(%Y年) | 第04周(2024年) |
数据库存储 | "%Y-%m-%d %H:%M:%S.%f" | 2024-06-01 14:30:45.123456 |
HTTP头日期 | "%a, %d %b %Y %H:%M:%S GMT" | Sat, 01 Jun 2024 06:30:45 GMT |
用户友好显示 | "%Y年%m月%d日 %H:%M" | 2024年06月01日 14:30 |
完整日期时间 | %A, %B %d, %Y %I:%M %p | Tuesday, January 23, 2024 03:30 PM |
六、平台差异注意
符号 | Windows支持 | Linux/macOS支持 | 注意事项 |
%z | Python 3.7+ | Python 3.3+ | 解析+08:00需Python 3.7+ |
%Z | 有限 | 有限 | 可能返回空字符串 |
%c | 依赖区域设置 | 依赖区域设置 | 不同系统输出格式不同 |