django - 如何减去两个日期并获得注释中的天数?
问题描述
我正在查询从一个人购买新手机之日到今天的天数。
device_record = Devc.objects.annotate(duration = todays_date - F('datebuy'))\
.order_by('datebuy')
当我检索数据时,我得到了这个3150 days, 20:08:00
。
我该怎么做才能删除时间,因为我只想显示number of days
?
我试过了:
device_record = Devc.objects.annotate(duration = (todays_date - F('datebuy')).days)\
.order_by('datebuy')
返回的错误:
'F' object has no attribute 'days'
我这样定义 todays_date todays_date = datetime.now().date()
,datebuy
即DateTimeField
解决方案
您可以尝试减去时间戳,然后检索日期。
from datetime import datetime, timedelta
now = datetime.now()
yesterday = now - timedelta(days=1)
duration = now - yesterday
print duration.days
输出:
1
推荐阅读
- linux - 在 linux 和 windows 中计算文件夹内容的哈希值
- c# - 通过资源设置数据上下文
- c# - 如何在asp.net核心的路由属性中设置正则表达式的匹配超时
- php - Doctrine ORM 协会创建 PHP 代理类
- python - 当我运行以下代码时,出现错误:
- ruby-on-rails - 如何使 sidekiq_retries_exhausted 在全球范围内可供所有工人使用
- ios - 更改 Swift 包中的默认标题注释许可证
- node.js - 无法从 OIDC 身份验证代码流中找到访问令牌
- amazon-web-services - 如果在 aws S3 中未收到文件,如何接收警报
- python - 将多个 csv 文件读入单独的数据帧循环