How to display current year in Flask template?

I want to learn how to output the current year to the Flask template. I know that in Django you can use {% now "Y" %}. but is there a Flask equivalent? So far I have not been able to find anything.

+5
source share
3 answers

Use the template context processor to pass the current date to each template and then display its year attribute.

 from datetime import datetime @app.context_processor def inject_now(): return {'now': datetime.utcnow()} 
 {{ now.year }} 

Or pass an object using render if it is not needed in most templates.

 return render_template('show.html', now=datetime.utcnow()) 
+14
source

Voting T Zhang There is currently Flask Moment .

It is powerful as a moment, and easy to use in a flask.

Show year (user time zone) in jinja2 template :

 <p>The current year is: {{ moment().format('YYYY') }}.</p> 
0
source

you can use jslib as moment.js as follows:

 <script> document.write(moment("2012-12-31T23:55:13 Z").format('LLLL')); </script> 
-1
source

Source: https://habr.com/ru/post/1261530/


All Articles