Cha*_*s R 2 python datetime-format python-3.x python-datetime
我正在运行 Python 3.8.3,我发现 ISO Week 格式 (%V) 有些奇怪:
2019 年的第一天和最后一天都在第一周。
from datetime import date
print(date(2019, 1, 1).strftime('%Y-W%V'))
print(date(2019, 12, 29).strftime('%Y-W%V'))
print(date(2019, 12, 31).strftime('%Y-W%V'))
Run Code Online (Sandbox Code Playgroud)
输出:
from datetime import date
print(date(2019, 1, 1).strftime('%Y-W%V'))
print(date(2019, 12, 29).strftime('%Y-W%V'))
print(date(2019, 12, 31).strftime('%Y-W%V'))
Run Code Online (Sandbox Code Playgroud)
为什么它会这样?
这是完全正确的。
正如您在日期中看到的,所有日期都在 中2019,因此使用 获取 2019 是正确的%Y。
周数由 ISO 定义,因此可以考虑上一年或明年的一周。
您需要使用%G获取年份的周数 ( %V)。