1.实现
def get_monday_to_sunday(today, weekly=0):
"""
:function: 获取指定日期的周一和周日的日期
:param today: '2021-11-16'; 当前日期:today = datetime.now().strftime('%Y-%m-%d')
:param weekly: 获取指定日期的上几周或者下几周,weekly=0当前周,weekly=-1上一周,weekly=1下一周
:return: 返回指定日期的周一和周日日期
:return_type: tuple
"""
last = weekly * 7
today = datetime.strptime(str(today), "%Y-%m-%d")
monday = datetime.strftime(today - timedelta(today.weekday() - last), "%Y-%m-%d")
monday_ = datetime.strptime(monday, "%Y-%m-%d")
sunday = datetime.strftime(monday_ + timedelta(monday_.weekday() + 6), "%Y-%m-%d")
return monday, sunday
2.测试
#### 输入1
today = '2021-11-16' # 指定的日期,或者当前日期=datetime.now().strftime('%Y-%m-%d'),这里的日期格式以“-”分隔
weekly = -1
print(get_monday_to_sunday(today, weekly))
#### 输出1
('2021-11-08', '2021-11-14') # 上一周
#### 输入2
today = '2021-11-16'
weekly = 0
print(get_monday_to_sunday(today, weekly))
#### 输出2
('2021-11-15', '2021-11-21') # 当前周
#### 输入3
today = '2021-11-16'
weekly = 1
print(get_monday_to_sunday(today, weekly))
#### 输出3
('2021-11-22', '2021-11-28') # 下一周