I'd use datetime.date()
instead to make it clear we are calculating dates here, and use date.weekday()
to get the current weekday instead of using the .isocalendar()
call, giving us a 0-based weekday number (0 is Monday).
import datetime
today = datetime.date(2013, 06, 26)
dates = [today + datetime.timedelta(days=i) for i in range(0 - today.weekday(), 7 - today.weekday())]
Demo:
>>> from pprint import pprint
>>> import datetime
>>> today = datetime.date(2013, 06, 26)
>>> pprint([today + datetime.timedelta(days=i) for i in range(0 - today.weekday(), 7 - today.weekday())])
[datetime.date(2013, 6, 24),
datetime.date(2013, 6, 25),
datetime.date(2013, 6, 26),
datetime.date(2013, 6, 27),
datetime.date(2013, 6, 28),
datetime.date(2013, 6, 29),
datetime.date(2013, 6, 30)]
On python 2 you can replace range()
with xrange()
if you like; for a 7-day value that won't make much difference.
Just to make it explicit; datetime.weekday()
exists as well, and there is a .isoweekday()
too, so there is no need to use .isocalendar()
anywhere.