在开发小程序时利用Moment.js格式时间

LeanCloud给的日期是ISO格式,比如2017-06-05T14:08:20.589Z,直接显示在页面上体验不好。

凡是有关日期的,格式化、计算,用moment就够了。

1.下载

选moment.min.js版本,因为没有用到国际化的东西,体积省点是一点。

2.集成

将moment.min.js放在小程序工程的utils目录下

3.调用

const moment = require('moment.min.js');
moment.locale('en', {
  longDateFormat : {
    l: "YYYY-MM-DD",
    L: "YYYY-MM-DD HH:mm"
  }
});
item.createdAt = moment(item.createdAt).format('L');

自定义一个长日期格式,方便外部调用,当然写作format('YYYY-MM-DD');也是没问题的,只是外部传参没有传一个'l'或'L'来得简洁

一点说明

that.setData({
  donateObjects: utils.dateFormat(donateObjects, 'l')
});

一般自己写的后端,约定的做法在是api格式好了再传给前端,而实际开发中用的是LeanCloud的JS库,能给的日期只能是ISO格式,而小程序的wxml偏偏还没弱,不能像主流MVVM框架那样提供filter/compute之类的过滤器,只有每每在.js中将数据遍历格式好了,再发给wxml渲染,例如

Moment.js是一个JavaScript的日期、时间处理工具类,其对于JavaScript的日期时间处理功能非常强悍和全面。可以用在浏览器环境中使用,也可以在Node.js中。Moment.jsMoment.js对Date对象的扩展不是使用Date.prototype ...

Moment.js是一个JavaScript的日期、时间处理工具类,其对于JavaScript的日期时间处理功能非常强悍和全面。可以用在浏览器环境中使用,也可以在Node.js中。

Moment.js

Moment.js对Date对象的扩展不是使用Date.prototype的方式,是对Date对象创建一个包装对象。通过moment()方法可以获取这个包装对象,并且可以在调用时传入一个支持的输入参数。

Moment.js 使用

获取当前日期时间,使用moment()方法即可,无需传入任何参数:

    var now = moment();

日期格式化

    console.log(moment().format('MMMM Do YYYY, h:mm:ss a')); // 输出:January 3rd 2017, 2:16:38 pm

    console.log(moment().format('dddd')); // 输出:Tuesday

    console.log(moment().format("MMM Do YY")); // 输出:Jan 3rd 17

    console.log(moment().format('YYYY [escaped] YYYY')); // 输出:2017 escaped 2017

    console.log(moment().format()); // 输出:2017-01-03T14:20:03+08:00

    console.log();

相对时间

    console.log(moment("20111031", "YYYYMMDD").fromNow()); // 输出:5 years ago

    console.log(moment("20120620", "YYYYMMDD").fromNow()); // 输出:5 years ago

    console.log(moment().startOf('day').fromNow()); // 输出:14 hours ago

    console.log(moment().endOf('day').fromNow()); // 输出:in 10 hours

    console.log(moment().startOf('hour').fromNow()); // 输出:22 minutes ago

    console.log();

日历时间

    console.log(moment().subtract(10, 'days').calendar()); // 输出:12/24/2016

    console.log(moment().subtract(6, 'days').calendar()); // 输出:Last Wednesday at 2:24 PM

    console.log(moment().subtract(3, 'days').calendar()); // 输出:Last Saturday at 2:24 PM

    console.log(moment().subtract(1, 'days').calendar()); // 输出:Yesterday at 2:24 PM

    console.log(moment().calendar()); // 输出:Today at 2:24 PM

    console.log(moment().add(1, 'days').calendar()); // 输出:Tomorrow at 2:24 PM

    console.log(moment().add(3, 'days').calendar()); // 输出:Friday at 2:24 PM

    console.log(moment().add(10, 'days').calendar()); // 输出:01/13/2017

    console.log();

多语言支持

    console.log(moment.locale()); // 输出:en

    var m = moment().locale('zh-cn');      

    console.log(m.format('LT')); // 输出:下午2点33分

    console.log(m.format('LTS')); // 输出:下午2点33分52秒

    console.log(m.format('L')); // 输出:2017-01-03

    console.log(m.format('l')); // 输出:2017-01-03

    console.log(m.format('LL')); // 输出:2017年1月3日

    console.log(m.format('ll')); // 输出:2017年1月3日

    console.log(m.format('LLL')); // 输出:2017年1月3日下午2点33分

    console.log(m.format('lll')); // 输出:2017年1月3日下午2点33分

    console.log(m.format('LLLL')); // 输出:2017年1月3日星期二下午2点33分

    console.log(m.format('llll')); // 输出:2017年1月3日星期二下午2点33分

猜你喜欢

转载自blog.csdn.net/QFire/article/details/84492696