【问题标题】:django difference between two timestamp in django templatedjango模板中两个时间戳之间的django区别
【发布时间】: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


【解决方案1】:

您可以使用timesince 模板标签来做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-18
    • 2012-05-29
    • 1970-01-01
    • 2019-10-03
    • 2020-12-10
    • 2014-04-14
    • 2012-02-29
    相关资源
    最近更新 更多