0
有人可以知道我可以做什麼倒數4週迴到給定的一年,然後計算每週的start_date和end_date?倒計時4周(一週一週)回到python/django
這是我現在有:從這個
def FourWeeks(year, week):
d = date(year, 1, 1)
delta_days = d.isoweekday() - 1
fourweekback = [week - x for x in range(4)]
for delta_weeks in fourweekback:
delta = timedelta(days=-delta_days, weeks=delta_weeks)
weekbeg = datetime.datetime.combine(d, datetime.time(00, 00, 01)) + delta
delta2 = timedelta(days=6-delta_days, weeks=delta_weeks)
weekend = datetime.datetime.combine(d, datetime.time(23, 59, 59)) + delta2
return (fourweekback, weekbeg, weekend)
輸出:
FourWeeks(2013, 44)
([40, 39, 38, 37], datetime.datetime(2013, 10, 7, 0, 0, 1), datetime.datetime(2013, 10, 13, 23, 59, 59))
([40, 39, 38, 37], datetime.datetime(2013, 9, 30, 0, 0, 1), datetime.datetime(2013, 10, 6, 23, 59, 59))
([40, 39, 38, 37], datetime.datetime(2013, 9, 23, 0, 0, 1), datetime.datetime(2013, 9, 29, 23, 59, 59))
([40, 39, 38, 37], datetime.datetime(2013, 9, 16, 0, 0, 1), datetime.datetime(2013, 9, 22, 23, 59, 59))
([40, 39, 38, 37], datetime.datetime(2013, 9, 16, 0, 0, 1), datetime.datetime(2013, 9, 22, 23, 59, 59))
,這就是我想實現:
FourWeeks(2013, 44)
(44, datetime.datetime(2013, 10, 28, 0, 0, 1), datetime.datetime(2013, 11, 3, 23, 59, 59))
(39, datetime.datetime(2013, 9, 23, 0, 0, 1), datetime.datetime(2013, 9, 29, 23, 59, 59))
(38, datetime.datetime(2013, 9, 16, 0, 0, 1), datetime.datetime(2013, 9, 22, 23, 59, 59))
(37, datetime.datetime(2013, 9, 9, 0, 0, 1), datetime.datetime(2013, 9, 15, 23, 59, 59))
在下一步我喜歡用weekbeg做個查詢,週末
cursor.execute("SELECT * FROM event WHERE datetime BETWEEN %s AND %s, [weekbeg, weekend])
任何想法我可以做到這一點?
根據什麼DBMS你'使用你可以將計算移動到SQL中...像這樣:http://stackoverflow.com/questions/7391718/select-records-from-one-week-previous-in-mysql –
此外 - 你的日期不要如果你定義了一個星期到底是什麼,那麼它會很有幫助(從特定的日子等開始......)我看不出你是如何得到10月28日在那裏 –
我會說我的一週開始星期一和結束星期天 – Robert