for d inrange(7,0,-1): left = (NOW - timedelta(days=d-1)).strftime('%m-%d') right = (NOW - timedelta(days=d-2)).strftime('%m-%d') print'from %s to %s' %(left,right)
但经过运行得到结果如下
1 2 3 4 5 6 7 8 9
from2015-03-1115:18:08.831342 to 2015-03-1215:18:08.831342 from2015-03-1215:18:08.831342 to 2015-03-1315:18:08.831342 from2015-03-1315:18:08.831342 to 2015-03-1415:18:08.831342 from2015-03-1415:18:08.831342 to 2015-03-1515:18:08.831342 from2015-03-1515:18:08.831342 to 2015-03-1615:18:08.831342 from2015-03-1615:18:08.831342 to 2015-03-1715:18:08.831342 from2015-03-1715:18:08.831342 to 2015-03-1815:18:08.831342
我做计算日期,应该是每天晚上0点到24点算为一天。所以改成如下模式,使获取的now为当天0点。
1 2 3 4 5 6 7 8 9 10 11
from dpythonatetime import datetime, timedelta
NOW = datetime.now() NOW = NOW.strftime('%Y-%m-%d') NOW = datetime.strptime(NOW, '%Y-%m-%d')
for d in range(7,0,-1): left = (NOW - timedelta(days=d-1)) right = (NOW - timedelta(days=d-2)) print 'from %s to %s' %(left,right)
之后通过一下函数,来获取一周每天拍摄的次数。
1 2 3 4 5 6 7 8 9 10 11
defgetWeeklyCount(): NOW = datetime.now() NOW = NOW.strftime('%Y-%m-%d') NOW = datetime.strptime(NOW, '%Y-%m-%d') last_7d_count = []
for d inrange(7,0,-1):#range(start,stop,step) count = Piclist.query.filter(Piclist.time.between(NOW - timedelta(days=d-1), NOW - timedelta(days=d-2))).count() thisday = (NOW - timedelta(days=d-1)).strftime('%m-%d') last_7d_count.append([thisday,count]) return last_7d_count