Moment.js是一个JavaScript的日期、时间处理工具类,其对于JavaScript的日期时间处理功能非常强悍和全面。可以用在浏览器环境中使用,也可以在Node.js中。
Moment.jsMoment.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分 参考资料Moment.js
Moment.js@github
Moment.js中文网
Moment.js中文文档系列
其他完整代码:https://github.com/guyoung/GyWxappCases/tree/master/Enhance
微信小程序Canvas增强组件WeZRender:https://github.com/guyoung/WeZRender