import datetime
# 大于等于昨天某个时间,即昨天这个时候到现在新增用户数
now = datetime.datetime.now()
yesterday = now - datetime.timedelta(hours=23, minutes=59, seconds=59)
user_nums = UserProfile.objects.filter(date_joined__gte=yesterday)
# 其他
# 大于:gt、小于:lt、小于等于:lte
today = datetime.datetime.now().day # 今天
yesterday = (datetime.datetime.now() - datetime.timedelta(days=1)).day # 昨天
# 查询今日、昨天的新增用户数量
UserProfile.objects.filter(date_joined__day=today).count()
# 查询时间段
start_date = datetime.date(2020, 4, 20)
end_date = datetime.date(2020, 5, 4)
user_nums = UserProfile.objects.filter(date_joined__range=(start_date, end_date)).count()
print(user_nums)
# 查询某年、某月、某日、周几
UserProfile.objects.filter(date_joined__year=2020)
UserProfile.objects.filter(date_joined__month=5)
UserProfile.objects.filter(date_joined__day=4)
UserProfile.objects.filter(date_joined__week_day=2)
参考文章:
https://blog.csdn.net/weixin_40907382/article/details/79242989
网友评论