程式碼示例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import datetime
import collections
def date_list_fun(starttime,endtime):
"""計算輸入的起始日期和結束日期之間的所有日期"""
_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
return date_list
def allweeks(year):
"""計算一年內所有周的具體日期,從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")
week_date={}
_time=start_date+n*_u
w=str(int(_time.strftime("%W"))+1)
week_date.setdefault(w,[]).append(_time.strftime("%Y%m%d"))
if _time==end_date:
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):
"""計算一年內所有周的具體日期,每週都是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")
y,w=_time.isocalendar()[:2]
if y==year :
# print week_date_start_end
return week_date,week_date_start_end
程式碼示例:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import datetime
import collections
def date_list_fun(starttime,endtime):
"""計算輸入的起始日期和結束日期之間的所有日期"""
_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
return date_list
def allweeks(year):
"""計算一年內所有周的具體日期,從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):
"""計算一年內所有周的具體日期,每週都是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