According to resources, the method to transform a string that contains "year-week_number" (such as 2015-20) to a datetime object is the following:
from datetime import datetime
datetime.strptime("2015-20", "%Y-%W")
However, this does not give me the correct result. What I am getting is datetime.datetime(2015, 1, 1, 0, 0)
Moreover:
datetime.strftime(datetime.strptime("2015-20", "%Y-%W"). "%Y-%W")
actually returns "2015-00".
Is this just a bug or did I forget to do something?
From the 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.
Given you are not specifying day of the week, the %W
parameter is being ignored as per the documentation.
Add the weekday to whatever you want, I put Monday:
>>> datetime.strptime("2015-20-1", "%Y-%W-%w")
datetime.datetime(2015, 5, 18, 0, 0)
Point 6. of the documentation is what's important here:
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.
from datetime import datetime
date = datetime.strptime("2016-12-0", "%Y-%m-%w")
print "Week:"m datetime.strftime(date, "%W")
>>>Week: 48
Also, be aware of the small difference between %W
and %U
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.