I am looking to find out how to output the current year in a Flask template. I know in Django you can use {% now "Y" %}.
, but is there a Flask equivalent? I have been unable to find anything during my research thus far.
Try return now. strftime("%d/%m/%y") docs.python.org/3/library/… I want to return it through date variable as you can see in my code. Put that line of code in your route.
You need to use datetime. now() and display it in one of your templates.
First, open the views.py file of your Django application and import the datetime module. Next, use the datetime. now() method to get the current date and time value.
Use a template context processor to pass the current date to every template, then render its year
attribute.
from datetime import datetime @app.context_processor def inject_now(): return {'now': datetime.utcnow()}
{{ now.year }}
Or pass the object with render
if you don't need it in most templates.
return render_template('show.html', now=datetime.utcnow())
For moment there is Flask Moment. It is powerful like Moment, and easy to use in Flask. To display the year in the user's local time from your Jinja template:
<p>The current year is: {{ moment().format('YYYY') }}.</p>
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