In Python, how do I find the date of the first Monday of a given week?

Question:

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

Asked By: Rui Vieira

||

Answers:

>>> import time
>>> time.asctime(time.strptime('2008 50 1', '%Y %W %w'))
'Mon Dec 15 00:00:00 2008'

Assuming the first day of your week is Monday, use %U instead of %W if the first day of your week is Sunday. See the documentation for strptime for details.

Update: Fixed week number. The %W directive is 0-based so week 51 would be entered as 50, not 51.

Answered By: Robert Gamble

This seems to work, assuming week one can have a Monday falling on a day in the last year.

from datetime import date, timedelta

def get_first_dow(year, week):
    d = date(year, 1, 1)
    d = d - timedelta(d.weekday())
    dlt = timedelta(days = (week - 1) * 7)
    return d + dlt
Answered By: gak

Use the string formatting found in the time module. Detailed explanation of the formats used

>>> import time
>>> time.strptime("51 08 1","%U %y %w")
(2008, 12, 22, 0, 0, 0, 0, 357, -1)

The date returned is off by one week according to the calendar on my computer, maybe that is because weeks are indexed from 0?

Answered By: Algorias

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:

from datetime import date, timedelta

def week_start_date(year, week):
    d = date(year, 1, 1)    
    delta_days = d.isoweekday() - 1
    delta_weeks = week
    if year == d.isocalendar()[0]:
        delta_weeks -= 1
    delta = timedelta(days=-delta_days, weeks=delta_weeks)
    return d + delta
Answered By: Vaidas K.

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.

from datetime import datetime, date, timedelta

def weekbegend(year, week):
    """
    Calcul du premier et du dernier jour de la semaine ISO
    """
    d = date(year, 1, 1)    
    delta_days = d.isoweekday() - 1
    delta_weeks = week
    if year == d.isocalendar()[0]:
        delta_weeks -= 1
    # delta for the beginning of the week
    delta = timedelta(days=-delta_days, weeks=delta_weeks)
    weekbeg = d + delta
    # delta2 for the end of the week
    delta2 = timedelta(days=6-delta_days, weeks=delta_weeks)
    weekend = d + delta2
    return weekbeg, weekend

Soyou can use it that way.

weekbeg, weekend = weekbegend(2009, 1)
begweek = weekbeg.strftime("%A %d %B %Y")
endweek = weekend.strftime("%A %d %B %Y")
Answered By: karlcow
from datetime import date, timedelta

def first_monday(year, week):
    d = date(year, 1, 4)  # The Jan 4th must be in week 1  according to ISO
    return d + timedelta(weeks=(week-1), days=-d.weekday())
Answered By: minkwe
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.