Moment.js is an easy-to-use lightweight JavaScript date processing library that provides features such as date formatting and date resolution. It supports running in two environments, both in the browser and Nodejs. Such libraries can convert any given date into a number of different formats, have powerful date calculations, and have built-in functions that can display a variety of date forms. In addition, it supports multiple languages and you can add a new language pack.
Flask-moment is a moment.js
Flask extension integrated into the JINJA2 template.
I. Initializing Flask-moment
from Import = Moment (APP)
Flask-moment dependent moment.js和
jquery.js
. Need to be included directly in the HTML document
Import Moment.js and Jquery.js in the head tag in the base.html template
<HTML> <Head>{{moment.include_jquery ()}} {{moment.include_moment () }}}<!--use Chinese, English is the default -{{Moment.lang ("ZH-CN")}}</Head> <Body>...</Body> </HTML>
If you use bootstrap, you can not import jquery.js, because Bootstrap contains jquery.js
Two. Using Flask-moment
In order to use flask-moment, you need to pass in a time variable to render to the template, such as:
from Import render_template from Import Date@main.route ('/') def index (): return render_template ('index.html', time = Date (1994,8,29))
Render in a template, such as:
< P > Current time: {{moment (). Format (' YYYY year M D Day, H:mm:ss a ')}}. </ P > < P > has passed: {{moment (). FromTime (Time)}}. </ P > < P > {{moment (). Calendar ()}}. </ P >
Results
Time: April 22, 2015, 10:06:33 a.m.. It's gone: 21 years. Today 10 o'clock in the morning 06.
In Moment (), if you do not pass in the python time variable, the UTC time conversion cost time is displayed as the default, which is passed in local=True参数可以关闭转换
.
Three. Common formatting parameters
YYYY |
2014 |
Year |
YY |
14 |
Year 2-character representation |
Q |
1..4 |
Quarter |
M MM |
4..04 |
Month |
MMM MMMM |
4月..四月 |
根据moment.locale()中的设置显示月份 |
D DD |
1..31 |
The day of the January |
Do |
1日..31日 |
The day of the January |
DDD DDDD |
1..365 |
The day ordinal of a year |
X |
1410715640.579 |
Time stamp |
x |
1410715640579 |
Time stamp |
The flask-moment of Flask learning record