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') ;
這樣就可以了。