7

I want to convert my timestamp to datetime in jinja2..

here's my sample code:

import time

date = time.time()
self.tv['date'] = date

sample html:

<p>{{ date }}</p>

I want to convert it to datetime using jinja2 in python..

thanks..

4

4 回答 4

14

制作一个自定义过滤器,例如

@app.template_filter('ctime')
def timectime(s):
    return time.ctime(s) # datetime.datetime.fromtimestamp(s)

并使用您的模板过滤器

{{ date | ctime }}
于 2015-02-23T11:57:43.977 回答
4

You convert it before passing it to a template, eg:

>>> import time
>>> date = time.time()
>>> from datetime import datetime
>>> datetime.fromtimestamp(date)
datetime.datetime(2013, 3, 1, 2, 57, 29, 472572)

And optionally use formatting:

>>> format(datetime.fromtimestamp(date), '%Y%m%d')
'20130301'
于 2013-03-01T02:58:09.157 回答
0

在模板中使用它:

{{ time | from_timestamp('%Y.%m.%d. %H:%M:%S UTC') }}
于 2020-09-22T16:12:20.743 回答
-2

使用以下,数据

{
    "timestamp": "1424197820"
}

模板

{{ timestamp|timestamp_to_time|datetimeformat('%a, %B %d') }}

渲染

2 月 17 日,星期二

资源

于 2019-09-18T02:21:40.477 回答