calendar by week

Peter Otten __peter__ at web.de
Thu May 27 06:08:37 EDT 2004


GuineaPig wrote:

> One thing that I would like to accomplish is that the user has to only
> give in the weeknumber of the year.  The app should then write the dates
> in this week to the excel-sheet.  I've been looking at the calendar and
> time modules but I don't quite see a solution there (maybe there is
> though, maybe I'm just too much of a newbie.)

import datetime

def daysOfWeek(year, week):
    day = datetime.date(year, 2, 1)
    year, weekBase, dayBase = day.isocalendar()
    day += datetime.timedelta(1 - dayBase + (week - weekBase)*7)
    delta = datetime.timedelta(1)
    for i in range(6):
        yield day
        day += delta
    yield day

Try it:

>>> from daysofweek import daysOfWeek
>>> for d in daysOfWeek(2004, 1):
...     print d
...
2003-12-29
2003-12-30
2003-12-31
2004-01-01
2004-01-02
2004-01-03
2004-01-04

Peter




More information about the Python-list mailing list