MyCalendar.py代码:
from django import template
imort calendar
import datetime
date = datetime.date.today()
week = ???
...
问题是我想得到包含今天日期的那一周。我该怎么办?
感谢您的帮助!
Ver:Django-1.0 Python-2.6.4
答案 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