简体   繁体   English

迄今为止的第一周数打印上一年

[英]first week number to date prints previous year

I want follow code to print 2020-01-01 00:00:00 instead of 2019-12-30.我想按照代码打印 2020-01-01 00:00:00 而不是 2019-12-30。

Any help would be appreciated.任何帮助,将不胜感激。

d = '2020-00'
r = datetime.datetime.strptime(d + '-1', "%Y-%W-%w")
print(r)
2019-12-30 00:00:00

2020 started on a Wednesday. 2020 年从一个星期三开始。 From this handy Python str[fp]time reference , %W stands for从这个方便的 Python str[fp]time reference中, %W代表

Week 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.第一个星期一之前的新年中的所有日子都被认为是在第 0 周。

while %w is%w

Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.工作日为十进制数,其中 0 是星期日,6 是星期六。

You passed the value 2020-00-1 , which corresponds to the 0th week's Monday, which was in fact 2019-12-30.您传递了值2020-00-1 ,它对应于第 0 周的星期一,实际上是 2019-12-30。

To make it result in 2020-01-01, do the following:要使其结果为 2020-01-01,请执行以下操作:

>>> import datetime
>>> d = "2020-00"
>>> r = datetime.datetime.strptime(d + "-3", "%Y-%W-%w")
>>> print(r)
2020-01-01 00:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM