Getting start/end dates given week-number

Serge Orlov Serge.Orlov at gmail.com
Fri Jun 9 12:54:05 EDT 2006


Tim Chase wrote:
> I've been trying to come up with a good algorithm for determining
> the starting and ending dates given the week number (as defined
> by the strftime("%W") function).

I think you missed %U format, since later you write:

> My preference would be for a Sunday->Saturday range rather than a
> Monday->Sunday range.  Thus,

> Any thoughts/improvements/suggestions would be most welcome.

If you want to match %U:

def weekBoundaries(year, week):
     startOfYear = date(year, 1, 1)
     week0 = startOfYear - timedelta(days=startOfYear.isoweekday())
     sun = week0 + timedelta(weeks=week)
     sat = sun + timedelta(days=6)
     return sun, sat




More information about the Python-list mailing list