django计算当前日期最近一周日期范围
导入包
from django.utils.timezone import now #时间
import datetime #时间
now_time = datetime.datetime.now() #获取当前时间
day_num = now_time.isoweekday() #当前天是这周的第几天
week_start = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=1)).date() #计算当前天所在周周一 格式为yyyy-MM-dd
week_end = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=7)).date() #计算当前天所在周周天 格式为yyyy-MM-dd
print(week_end)
week_e = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=7)) #计算当前天所在周周天 格式为yyyy-MM-dd 12:01:13.026315
week_s = ((now_time - datetime.timedelta(days=day_num)) + datetime.timedelta(days=1))#计算当前天所在周周一 格式为yyyy-MM-dd 12:01:13.026315
while week_s <= week_e: #循环输出周一至周五的每天日期格式yyyy-MM-dd
print(week_s.date())
week_s = week_s + datetime.timedelta(days=1)
create_time__range=(week_start, week_end) #范围(周一,周五)可用于数据库日期范围查询
print(create_time__range)