摘要:本文将带你了解微信开发之小程序框架功能:日期时间Moment.js,希望本文对大家学微信有所帮助。
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分
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之微信频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号