Flask-Moment本地化日期和時間

moment.js客戶端開源代碼庫,能夠在瀏覽器中渲染日期和時間。Flask-Moment是一個flask程序擴展,能把moment.js集成到Jinja2模板中。html

 

一、安裝flask

pip install flask-momentbootstrap

 

二、初始化Flask-Moment瀏覽器

from flask_moment import Momentapp

moment = Moment(app)學習

除了moment.js,Flask-Moment還依賴jQuery.js。安裝了flask-bootstrap(pip install flask-bootstrap),因爲Bootstrap已經引入了jQuery.js,所以只需引入moment.js便可。3d

 

三、template/base.html:引入moment.js庫orm

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

 

四、hello.py:加入一個datetime變量blog

from datetime import datetime

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

 

五、templates/index.html: 使用Flask-Moment渲染時間戳

<p>The local date and time is {{ moment(current_time).format('LLL') }}.</p>
<p>That was {{ moment(current_time).fromNow(refresh=True) }}.</p>

注:可查閱文檔(http://momentjs.com/docs/#/displaying/)學習moment.js提供的所有格式化選項。

 

六、結果截圖

相關文章
相關標籤/搜索