How can I find the "week" in a django calendar app?

MyCalendar.py code:

from django import template
imort calendar
import datetime

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

      

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

Thanks for the help!

Ver: Django-1.0 Python-2.6.4

+2


a source to share


2 answers


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

      



Printing

[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)]

      

+11


a source


If you want the week number ie 0-53 try the method isocalendar

.

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

      



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

+3


a source







All Articles