如何获得当前的时间在jinja

Vik*_*ram 9 jinja2

获取jinja标签中当前日期时间值的方法是什么?

在我的项目中,我需要在站点的右上角显示UTC的当前时间.

Bru*_*sky 12

我喜欢@ assem-chelli的回答.我将在Jinja2模板中演示它.

#!/bin/env python3
import datetime
from jinja2 import Template

template = Template("""
# Generation started on {{ now() }}
... this is the rest of my template...
# Completed generation.
""")

template.globals['now'] = datetime.datetime.utcnow

print(template.render())
Run Code Online (Sandbox Code Playgroud)

输出:

# Generation started on 2017-11-14 15:48:06.507123
... this is the rest of my template...
# Completed generation.
Run Code Online (Sandbox Code Playgroud)


Big*_*her 7

您应该使用datetimePython库,获取时间并将其作为变量传递给模板:

>>> import datetime
>>> datetime.datetime.utcnow()
'2015-05-15 05:22:17.953439'
Run Code Online (Sandbox Code Playgroud)