这篇文章主要介绍了django如何计算日期,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
import datetime,timedelta import calendar
now = datetime.now() today = datetime.now().date()
# 今日if days == "1": quesryset = DailyReport.objects.filter(createdate__gte=str(today) + ' 00:00:00')# 昨天elif days == "2": end = (now + timedelta(days=-1)).date() quesryset = DailyReport.objects.filter(createdate__gte=str(end) + ' 00:00:00', createdate__lte=str(end) + ' 23:59:59')# 本周elif days == "3": end = (now + timedelta(days=-now.isoweekday())).date() quesryset = DailyReport.objects.filter(createdate__gte=str(end) + ' 00:00:00', createdate__lte=str(today) + ' 23:59:59')# 上周elif days == "4": end = (now - timedelta(days=7 + now.isoweekday())).date() today = (now - timedelta(days=now.isoweekday()+1)).date() quesryset = DailyReport.objects.filter(createdate__gte=str(end) + ' 00:00:00', createdate__lte=str(today) + ' 23:59:59')# 本月elif days == "5": end = (now - timedelta(days=now.day)).date() quesryset = DailyReport.objects.filter(createdate__gte=str(end) + ' 00:00:00', createdate__lte=str(today) + ' 23:59:59')# 上月elif days == "6": last_month = now - timedelta(days=now.day) end_month_days = calendar.monthrange(last_month.year, last_month.month)[1] end = (last_month - timedelta(days=end_month_days)).date() today = (now - timedelta(days=now.day+1)).date() quesryset = DailyReport.objects.filter(createdate__gte=str(end) + ' 00:00:00', createdate__lte=str(today) + ' 23:59:59')# 过去七天elif days == "7": end = (now - timedelta(days=7)).date() quesryset = DailyReport.objects.filter(createdate__gte=str(end) + ' 00:00:00', createdate__lte=str(today) + ' 23:59:59')# 过去30天elif days == "8": end = (now - timedelta(days=30)).date() quesryset = DailyReport.objects.filter(createdate__gte=str(end) + ' 00:00:00', createdate__lte=str(today) + ' 23:59:59')
感谢你能够认真阅读完这篇文章,希望小编分享的“django如何计算日期”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。