python获取本周日期范围

import datetime

today = datetime.date.today()
# 周一为第一天
week_start = today - datetime.timedelta(days=today.weekday())
week_end = today + datetime.timedelta(days=6-today.weekday())
print('week_start = {}\nweek_end = {}'.format(week_start,week_end))
# 周日为第一天
week_start = today - datetime.timedelta(days=today.weekday()+1)
week_end = today + datetime.timedelta(days=6-today.weekday()-1)
print('week_start = {}\nweek_end = {}'.format(week_start,week_end))

 

posted @ 2022-05-12 18:37  OTAKU_nicole  阅读(600)  评论(0编辑  收藏  举报