根据资源,将包含“year-week_number”(如2015-20)的字符串转换为datetime对象的方法如下:
from datetime import datetime
datetime.strptime("2015-20", "%Y-%W")
但是,这并没有给我正确的结果。我得到的是 日期时间.日期时间(2015, 1, 1, 0, 0)
另外:
datetime.strftime(datetime.strptime("2015-20", "%Y-%W"). "%Y-%W")
实际返回“2015-00”。
这只是一个错误还是我忘记做某事了?
最佳答案
来自documentation :
When used with the strptime() method, %U and %W are only used in calculations when the day of the week and the year are specified.
假设您没有指定星期几,根据文档,%W
参数将被忽略。
将工作日添加到任何你想要的,我把星期一:
>>> datetime.strptime("2015-20-1", "%Y-%W-%w")
datetime.datetime(2015, 5, 18, 0, 0)
关于带有日历周数的 Python datetime strptime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41753297/