If I have a certain week number (eg 51) and a given year (eg 2008), how do I find the date of the first Monday of that same week?
Many thanks
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
|
Assuming the first day of your week is Monday, use Update: Fixed week number. The |
|||||||||
|
|
PEZ's and Gerald Kaszuba's solutions work under assumption that January 1st will always be in the first week of a given year. This assumption is not correct for ISO calendar, see Python's docs for reference. For example, in ISO calendar, week 1 of 2010 actually starts on Jan 4, and Jan 1 of 2010 is in week 53 of 2009. An ISO calendar-compatible solution:
|
|||
|
|||||||||
|
|
This seems to work, assuming week one can have a Monday falling on a day in the last year.
|
|||
|
|
|
I have slightly modified the script of Vaidas K. in a way that it will return the beginning of the week and the end day of the week.
Soyou can use it that way.
|
|||||
|
|
Use the string formatting found in the time module. Detailed explanation of the formats used
The date returned is off by one week according to the calendar on my computer, maybe that is because weeks are indexed from 0? |
|||
|
|