import datetime
from dateutil.relativedelta import relativedelta
yesterday_time = datetime.datetime.today() - datetime.timedelta(days=1)
#判断前一天是否是工作日 isoweekday()函数返回的值,1代表周一,2代表周二,以此类推。
if yesterday_time.isoweekday() not in [1, 2, 3, 4, 5]:
yesterday_time = yesterday_time.isoweekday() - 6 and yesterday_time - datetime.timedelta(
days=2) or yesterday_time - datetime.timedelta(days=1)
yesterday_time = yesterday_time
else:
yesterday_time = yesterday_time
print('前一个工作日期:', yesterday_time.strftime("%Y%m%d"))
date_list = []
return_1w = yesterday_time - datetime.timedelta(days=7)+ datetime.timedelta(days=1)
date_list.append(return_1w)
return_1m = yesterday_time - relativedelta(months=1) + datetime.timedelta(days=1)
date_list.append(return_1m)
return_3m = yesterday_time - relativedelta(months=3) + datetime.timedelta(days=1)
date_list.append(return_3m)
return_6m = yesterday_time - relativedelta(months=6) + datetime.timedelta(days=1)
date_list.append(return_6m)
return_1y = yesterday_time - relativedelta(years=1) + datetime.timedelta(days=1)
date_list.append(return_1y)
return_3y = yesterday_time - relativedelta(years=3) + datetime.timedelta(days=1)
date_list.append(return_3y)
return_5y = yesterday_time - relativedelta(years=5) + datetime.timedelta(days=1)
date_list.append(return_5y)
time_list = []
#判断1周前,1月前,3月前,6月前,1年前....,得工作日
for i in date_list:
if i.isoweekday() not in [1, 2, 3, 4, 5]:
j = i.isoweekday() - 6 and i + datetime.timedelta(days=1) or i + datetime.timedelta(days=2)
time_list.append(j.strftime("%Y%m%d"))
else:
time_list.append(i.strftime("%Y%m%d"))
print(time_list)