vue-cli 利用moment.js转化时间格式为YYYY年MM月DD日,或者是YYYY-MM-DD HH:MM:SS 等格式

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>

猜你喜欢

转载自www.cnblogs.com/panax/p/10932550.html