【发布时间】:2018-12-28 13:14:34
【问题描述】:
假设,我有一个时间戳2018-07-20 14:47:12.984313+06,我需要计算与 Django 模板中的2018-07-20 14:50:12.984313+06 的差异。它应该返回 3 作为结果。
我该怎么做?
【问题讨论】:
-
在模板中?一般来说,在模板中进行计算是一种反模式。
-
您可能可以使用
timesince模板过滤器:docs.djangoproject.com/en/dev/ref/templates/builtins/#timesince 但它会返回以小时、分钟等表示的时间。 -
我已经尝试过 timesince 但它不适合我。我需要与时间戳值进行比较才能在 if else 块中使用它的值。
-
@WillemVanOnsem 是的,我在模板上需要它。我尝试查询类似这样的 annotate(minute=ExtractMinute('created_at')) 并从时间戳返回分钟,例如 2018-07-20 14:47:12.984313+06 给我 47。
标签: django django-templates django-template-filters