What is the method to get current date time value in jinja
tags ?
On my project I need to show current time in UTC at top right corner of site.
Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.
I like @assem-chelli's answer. I'm going to demo 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())
Output:
# Generation started on 2017-11-14 15:48:06.507123
... this is the rest of my template...
# Completed generation.
You should use datetime
library of Python, get the time and pass it as a variable to the template:
>>> import datetime
>>> datetime.datetime.utcnow()
'2015-05-15 05:22:17.953439'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With