[Python] 纯文本查看 复制代码 from datetime import datetime,timedelta
#copyright python36
def get_week_day(datestr):
week_day_dict={0:-1,1:-2,2:-3,3:-4,4:-5,5:-6,6:0} #周日到周六为一周
#week_day_dict={0:0,1:-1,2:-2,3:-3,4:-4,5:-5,6:-6} #周一到周日为一周
day=datestr.weekday()
return week_day_dict[day]
def week_all(ystr,wcount):
firststr=get_week_day(datetime.strptime(ystr+'.01.01',"%Y.%m.%d"))
publicstr=''
for i in range(0,wcount) :
if i==0:
firststr=(datetime.strptime(ystr+'.01.01',"%Y.%m.%d")+timedelta(days=firststr)).strftime("%Y.%m.%d")
publicstr=(datetime.strptime(firststr,"%Y.%m.%d")+timedelta(days=6)).strftime("%Y.%m.%d")
print(firststr+'-'+publicstr)
elif (i%2)==0:
evenstr=(datetime.strptime(publicstr,"%Y.%m.%d")+timedelta(days=1)).strftime("%Y.%m.%d")
publicstr=(datetime.strptime(evenstr,"%Y.%m.%d")+timedelta(days=6)).strftime("%Y.%m.%d")
print(evenstr+'-'+publicstr)
else:
oddstr=(datetime.strptime(publicstr,"%Y.%m.%d")+timedelta(days=1)).strftime("%Y.%m.%d")
publicstr=(datetime.strptime(oddstr,"%Y.%m.%d")+timedelta(days=6)).strftime("%Y.%m.%d")
print(oddstr+'-'+publicstr)
#传入年份,输入要生成周期数
week_all('2019',10)
运行效果:
2018.12.30-2019.01.05
2019.01.06-2019.01.12
2019.01.13-2019.01.19
2019.01.20-2019.01.26
2019.01.27-2019.02.02
2019.02.03-2019.02.09
2019.02.10-2019.02.16
2019.02.17-2019.02.23
2019.02.24-2019.03.02
2019.03.03-2019.03.09 |