views:

111

answers:

2

MyCalendar.py Code:

from django import template
imort calendar
import datetime

date = datetime.date.today()
week = ???
...

The question is that I want to get the week which contains today's date. How can I do?

Thanks for help!

Ver: Django-1.0 Python-2.6.4

+1  A: 

If you want the week number i.e. 0-53 try the isocalendar method.

date=datetime.date.today()
week=date.isocalendar()[1]

http://docs.python.org/library/datetime.html#datetime.datetime.isocalendar

czarchaic
Um...All right, I'm sorry I don't want to get the week No. What I've tried to do is to get a week list. For example if today's date is May.6, I want to get a list like [3,4,5,6,7,8,9] as a week list. So, can you give me another solution to solve this problem? Thanks.
Malcom.Z
+1  A: 

After reading your comment I think this is what you want:

import datetime

today = datetime.date.today()
weekday = today.weekday()
start_delta = datetime.timedelta(days=weekday)
start_of_week = today - start_delta
week_dates = [start_of_week + datetime.timedelta(days=i) for i in range(7)]
print week_dates

Prints:

[datetime.date(2010, 5, 3), datetime.date(2010, 5, 4), datetime.date(2010, 5, 5), datetime.date(2010, 5, 6), datetime.date(2010, 5, 7), datetime.date(2010, 5, 8), datetime.date(2010, 5, 9)]
miles82
Well! That's it! Thanks for your answer. : )
Malcom.Z