Flask-Moment 时间戳渲染

Web程序使用Flask-Moment显示本地化日期与时间

在hello.py中初始化Flask-Moment

from flask_moment import Moment
moment = Moment(app)

在templates/base.html中引入moment.js库

{% block scripts %}
{{ super() }}
{{ moment.include_moment() }}
{% endblock %}

在hello.py中加入一个datetime变量

from datetime import datetime
@app.route('/')
def index():
return render_template('index.html',current_time=datetime.utcnow())

在templates/index.html当中渲染current_time

{% extends 'base.html' %}
{% block page_content %}

The local date and time is {{ moment(current_time).format('LLL') }}.

That was {{ moment(current_time).fromNow(refresh=True) }}

{% endblock %}

实现效果:
第一行显示当前时间,第二行显示呆在这个页面的时间


时间渲染效果

你可能感兴趣的:(Flask-Moment 时间戳渲染)