Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current time in jinja

Tags:

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.

like image 806
Vikram Avatar asked May 07 '15 15:05

Vikram


People also ask

What is the difference between Jinja and Jinja2?

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.


2 Answers

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.
like image 113
Bruno Bronosky Avatar answered Sep 17 '22 19:09

Bruno Bronosky


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'
like image 20
Assem Avatar answered Sep 17 '22 19:09

Assem