#!/usr/bin/env python # -*- coding:utf-8 -*- import datetime import collections def get_date_list(starttime='20151101',endtime='20151210'): '''計算輸入的起始日期和結束日期之間的全部日期''' datelist=[] d1 = datetime.datetime(int(starttime[0:4]),int(starttime[4:6]),int(starttime[6:8])) d2 = datetime.datetime(int(endtime[0:4]),int(endtime[4:6]),int(endtime[6:8])) for i in range((d2-d1).days+1): d3=d1+datetime.timedelta(days=i) datelist.append(str(d3.strftime("%Y%m%d"))) print datelist return datelist def date_list_fun(starttime='20151101',endtime='20151210'): '''計算輸入的起始日期和結束日期之間的全部日期''' _u=datetime.timedelta(days=1) startdate=datetime.datetime.strptime(starttime,'%Y%m%d') enddate=datetime.datetime.strptime(endtime,'%Y%m%d') n=0 date_list=[] if startdate<=enddate: # print startdate,enddate while 1: _time=startdate+n*_u date_list.append(_time.strftime('%Y%m%d')) n=n+1 if _time==enddate: break print date_list return date_list def allweeks(year=2015): '''計算一年內全部周的具體日期,從1月1號開始,12.31號結束 輸出如{1: ['20190101','20190106'],...} 只有六天 ''' start_date=datetime.datetime.strptime(str(year)+'0101','%Y%m%d') end_date=datetime.datetime.strptime(str(year)+'1231','%Y%m%d') _u=datetime.timedelta(days=1) n=0 week_date={} while 1: _time=start_date+n*_u w=str(int(_time.strftime('%W'))+1) week_date.setdefault(w,[]).append(_time.strftime('%Y%m%d')) n=n+1 if _time==end_date: break week_date_start_end={} for i in week_date: week_date_start_end[i]=[week_date[i][0],week_date[i][-1]] print week_date print week_date_start_end return week_date def all_weeks(year=2015): '''計算一年內全部周的具體日期,每週都是7天,可能最後一週到 下年 week_date 輸出如{1: ['20181231', '20190101', '20190102', '20190103', '20190104', '20190105', '20190106'],...} 計算一年內全部周的起始日期 week_date_start_end {1: ['20181231','20190106'],...} ''' start_date=datetime.datetime.strptime(str(int(year)-1)+'1224','%Y%m%d') end_date=datetime.datetime.strptime(str(int(year)+1)+'0107','%Y%m%d') _u=datetime.timedelta(days=1) n=0 week_date={} while 1: _time=start_date+n*_u y,w=_time.isocalendar()[:2] if y==year : week_date.setdefault(w,[]).append(_time.strftime('%Y%m%d')) n=n+1 if _time==end_date: break week_date_start_end={} for i in week_date: week_date_start_end[i]=[week_date[i][0],week_date[i][-1]] print week_date # print week_date_start_end return week_date,week_date_start_end