Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display current year in Flask template?

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.

like image 601
kbdev Avatar asked Dec 19 '16 21:12

kbdev


People also ask

How do I find the current date on a Flask?

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.

How do I display the time on my Flask?

You need to use datetime. now() and display it in one of your templates.

How do I get current year in Django?

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.


2 Answers

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()) 
like image 136
davidism Avatar answered Oct 10 '22 21:10

davidism


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> 
like image 37
tim Avatar answered Oct 10 '22 21:10

tim