The flask-moment of Flask learning record

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.