如何从数据库中减去当前日期和日期

时间:2017-06-19 10:59:06

标签: python mysql django date subtraction

我有一个带有lastAttendance列的捐赠者表。如何用选定的lastAttendance日期减去当前日期?

d0 = Donor.objects.only("lastAttendance")
d1 = datetime.now()
delta = d1 - d0

错误:

unsupported operand type(s) for -: 'datetime.datetime' and 'QuerySet'

3 个答案:

答案 0 :(得分:0)

尝试以下代码

from django.utils import timezone  
d0 = Donor.objects.only("lastAttendance")[0]
d1 = timezone.now()
delta = d1 - d0.lastAttendance

答案 1 :(得分:0)

Django有一个时间功能。在文档中阅读它。 https://docs.djangoproject.com/en/1.11/ref/templates/builtins/#timesince

答案 2 :(得分:0)

如果您想在几天内得到答案,那么这可能就是您所寻找的:

d1 = datetime.now()  //---------------current date
d2 = datetime.strptime(d2, "%Y-%m-%d")
abs((d1 - d2).days)