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 %}
<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}</p>
{% endblock %}
实现效果:
第一行显示当前时间,第二行显示呆在这个页面的时间

网友评论