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>

 方法二:

 当然在某些特殊业务下,上面那种方法竟然没渲染出来,可以试试下面这种:

var moment = require('moment') 引入页面某一组件内
this.visitorTime = moment(val).format('YYYY-MM-DD HH:mm:ss') ;
这样就可以了。



原文地址:https://www.cnblogs.com/panax/p/10932550.html