moment.js 处理时间格式

时间:2021-06-23 17:15:52   收藏:0   阅读:0

1.在mian.js引入moment

import moment from ‘moment‘
Vue.prototype.$moment = ‘moment‘

2. 在main.js 设置全局过滤器

Vue.filter(‘moment‘, function (value, formatString) {
formatString = formatString || ‘YYYY年MM月DD日‘;
// return moment(value).format(formatString); // value可以是普通日期 20170723
return moment.unix(value).format(formatString); // 这是时间戳转时间
});//标红处为格式的自定义 同样可以YYYY-MM-DD HH:MM:SS ,或者 YYYY/MM/DD

3.渲染到页面

 <div>{{time | moment}}</div


4.也可以写一个方法,通过方法返回

// 时间格式化处理
computeData(date, rule) {
if (rule) {
return moment(date).format(rule)
} else {
return moment(date).format(‘YYYY年MM月DD日‘)
}
},
引用:{{computeData(20211010,‘可选‘)}}
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!