How to display current year in Flask template?

前端 未结 3 672
暗喜
暗喜 2021-02-07 06:09

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

相关标签:
3条回答
  • 2021-02-07 06:44

    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())
    
    0 讨论(0)
  • 2021-02-07 06:53

    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>
    
    0 讨论(0)
  • 2021-02-07 07:01

    You can use a JavaScript library like moment.js like this:

    <script>
    document.write(moment("2012-12-31T23:55:13 Z").format('LLLL'));
    </script>
    
    0 讨论(0)
提交回复
热议问题