【问题标题】:How to filter list with Remaining days of date of birth in Django如何在Django中使用剩余的出生日期过滤列表
【发布时间】:2021-12-08 00:28:39
【问题描述】:

我想显示一个即将到来的生日列表,剩余天数为 30 天。
示例:

Name Date Of Birth Remaining Days
John Die 2050-10-25 4 Days Left
John Snow 2050-10-26 5 Days Left

我不知道如何计算剩余天数,但我尝试了这段代码,但出现错误“无法减去偏移天真和偏移感知日期时间”

customer=Customer.objects.filter()[:10]
    for i in customer:
        remaining = i.dateOfBirth - datetime.today()
        print(remaining)

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    您可以从datetime.today() 中删除tzinfo

    remaining = i.dateOfBirth - datetime.today().replace(tzinfo=None)
    

    或添加到i.dateOfBirth

    remaining = i.dateOfBirthreplace(tzinfo=datetime.timezone.utc) - datetime.today()
    

    更多信息:https://code.luasoftware.com/tutorials/python/python-typeerror-cant-subtract-offset-naive-and-offset-aware-datetimes/

    【讨论】:

      猜你喜欢
      • 2014-06-15
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 2010-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-09-08
      • 1970-01-01
      相关资源
      最近更新 更多