【问题标题】:converting long format time into proper time in django templtes list?将长格式时间转换为 django 模板列表中的适当时间?
【发布时间】:2017-05-05 20:54:14
【问题描述】:

我对来自 API 的长时间格式的每个条目都有一个时间值。所以我想将其转换为人类可读的形式。

我的 API 值是:-

"data": [
    {
      "id": 33613,
      "virtualNewsId": 30513,
      "newsCommentText": "@39586!~~!Abhay_raj_singh_chauhan!~~!Abhay%20Raj%20%20!>>! @39586!~~!Abhay_raj_singh_chauhan!~~!Abhay%20Raj%20%20!>>!",
      "user": {
        "id": 39561,
        "name": "chatuser",
        "username": "chatuser",
        "email": "chatuser@mail.com",
        "profileImage": "",
        "profileImageThumbnail": "",
        "isfollow": false
      },
      "image": null,
      "createdAt": 1493978312000
    }

现在我如何在 django 模板中转换 createdAt 人类可读的 forn。我有很多这样的值和 uaing forloop 以显示在表格上。那么我该如何实现这一点。

【问题讨论】:

标签: python django django-templates


【解决方案1】:

您可以在 django 中通过编写 simple tag 来执行此操作,将您的时间戳解析为人类可读的格式。

{% parsetimestamp createdAt %}

import datetime
from django import template

register = template.Library()

@register.simple_tag(name='parsetimestamp')
def parser(value):
    return datetime.datetime.fromtimestamp(value/1000)

或者用 Javascript 简单地做:

<span>
   <script>document.write(Date({{ createdAt }}))</script>
</span>

【讨论】:

  • 过滤器可能更好,因为默认的date 可以与它​​链接在一起。
猜你喜欢
  • 2023-03-20
  • 1970-01-01
  • 2021-09-29
  • 2023-04-09
  • 1970-01-01
  • 2012-07-01
  • 2021-05-17
  • 1970-01-01
  • 2017-05-23
相关资源
最近更新 更多