我想获得非常久远零填充一周如下的ISO日历格式的当前日期?
2019/W06
我尝试以下,但使用strftime
喜欢的东西,因为它是更容易阅读。
print(str(datetime.datetime.today().isocalendar()[0]) + '/W' + str(datetime.datetime.today().isocalendar()[1]))
2019/W6
使用下面的代码:
print(datetime.now().strftime('%Y/W%V'))
%Y年份以世纪为十进制数。
%V - 在ISO 8601的周数本年度(01至53),其中第1周是在本年度至少4天的第一周,星期一作为一周的第一天。
https://docs.python.org/3.7/library/datetime.html#strftime-and-strptime-behavior
解决方案与strftime
:
如果你想补零:
datetime.date.today().strftime("%Y/W%V")
输出:
2019/W06
如果你不希望它:
datetime.date.today().strftime("%Y/W%-V")
输出:
2019/W6
请注意,“%V”返回的周数,而“ - ”是什么东西删除前导零。