How to get the current time in Jinja

What is the method of getting the current date value in tags jinja?

In my project, I need to show the current time in UTC in the upper right corner of the site.

+4
source share
2 answers

You should use the datetimePython library , get the time and pass it as a variable to the template:

>>> import datetime
>>> datetime.datetime.utcnow()
'2015-05-15 05:22:17.953439'
+5
source

I like @ assem-chelli answer. I am going to demonstrate it in a Jinja2 template.

#!/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())

Conclusion:

# Generation started on 2017-11-14 15:48:06.507123
... this is the rest of my template...
# Completed generation.
+4
source

All Articles