文章内容

2018/1/12 14:15:50,作 者: 黄兵

moment(current_time).format('L')

最近在使用Python Flask制作网站的时候,需要根据客户的时区获取当前时间,使用的是Moment.js

后端代码如下:

from flask import Flask , render_template
from flask.ext.moment import Moment
from datetime import datetime

app = Flask(__name__)
moment=Moment(app)

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


if __name__ == '__main__':
app.run()

获取具体时间格式如下:

Format Dates

moment().format('MMMM Do YYYY, h:mm:ss a'); // 一月 12日 2018, 2:08:48 下午
moment().format('dddd'); // 星期五
moment().format("MMM Do YY"); // 1月 12日 18
moment().format('YYYY [escaped] YYYY'); // 2018 escaped 2018
moment().format(); // 2018-01-12T14:08:48+08:00

Relative Time


moment("20111031", "YYYYMMDD").fromNow(); // 6 年前
moment("20120620", "YYYYMMDD").fromNow(); // 6 年前
moment().startOf('day').fromNow(); // 14 小时前
moment().endOf('day').fromNow(); // 10 小时内
moment().startOf('hour').fromNow(); // 12 分钟前

Calendar Time

moment().subtract(10, 'days').calendar(); // 2018/01/02
moment().subtract(6, 'days').calendar(); // 上星期六14:13
moment().subtract(3, 'days').calendar(); // 上星期二14:13
moment().subtract(1, 'days').calendar(); // 昨天14:13
moment().calendar(); // 今天14:13
moment().add(1, 'days').calendar(); // 明天14:13
moment().add(3, 'days').calendar(); // 下星期一14:13
moment().add(10, 'days').calendar(); // 2018/01/22

Multiple Locale Support

moment.locale();         // zh-cn
moment().format('LT'); // 14:14
moment().format('LTS'); // 14:14:32
moment().format('L'); // 2018/01/12
moment().format('l'); // 2018/1/12
moment().format('LL'); // 2018年1月12日
moment().format('ll'); // 2018年1月12日
moment().format('LLL'); // 2018年1月12日下午2点14分
moment().format('lll'); // 2018年1月12日 14:14
moment().format('LLLL'); // 2018年1月12日星期五下午2点14分
moment().format('llll'); // 2018年1月12日星期五 14:14

参考资料:Moment.js

分享到:

发表评论

评论列表