如何在django的日历应用程序中找到“周”?

时间:2010-05-06 05:42:27

标签: python django calendar

MyCalendar.py代码:

from django import template
imort calendar
import datetime

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

问题是我想得到包含今天日期的那一周。我该怎么办?

感谢您的帮助!

Ver:Django-1.0 Python-2.6.4

2 个答案:

答案 0 :(得分:11)

阅读完评论后,我认为这就是你想要的:

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

打印:

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

答案 1 :(得分:3)

如果您想要周数,即0-53,请尝试isocalendar方法。

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

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