Malcom.Z
Malcom.Z

Reputation: 77

How can I find "week" in django's calendar app?

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

Upvotes: 2

Views: 2863

Answers (2)

miles82
miles82

Reputation: 6794

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

Upvotes: 11

czarchaic
czarchaic

Reputation: 6318

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

Upvotes: 3

Related Questions