views:

2218

answers:

9

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

A: 

Look at the documentation here

The rest is up to you.

devin
+12  A: 
>>> 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.

Robert Gamble
Oooh, magic, I like it!
Lasse V. Karlsen
+4  A: 

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
Gerald Kaszuba
A: 

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?

Algorias
+9  A: 

Week 51 of 2008 started with Monday December 15:

>>> d = date(2008, 1, 1)
>>> d + timedelta(days=-d.weekday(), weeks=50)
datetime.date(2008, 12, 15)

Week 1 of 2009 started on December 29:

>>> d = date(2009, 1, 1)
>>> d + timedelta(days=-d.weekday(), weeks=0)
datetime.date(2008, 12, 29)

Found a web calendar that shows week numbers. And then there's always vecka.nu (Vecka means week in Swedish, nu means now.)

PEZ
Elegant solution. Thanks.
Rui Vieira
Wait...what? This is week 1? Sure felt like 52.
Mark
Dec 31 2009 was week 53, wasn't it?
PEZ
+3  A: 

Anybody interested in these sorts of questions should check out the smash-hit bestseller Calendrical Calculations by Reingold and Dershowitz. Before there were books there was a journal paper, which is the most requested reprint in the history of Software---Practice & Experience.

Norman Ramsey
+4  A: 

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
Vaidas K.
A: 

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")
karlcow
+1  A: 
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())
minkwe
This one is clever!
WooYek