在main.js中全局注册过滤器
// 全局注册自定义的日期过滤器dataFormat,传参值originVal为毫秒
Vue.filter('dateFormat',function(originVal){
// 先把传参毫秒转化为new Date()
const dt = new Date(originVal)
const y = dt.getFullYear()
// 月份是从0开始,需要+1。 +''是把数字转化为字符串,padStart(2,'0')是把字符串设置为2位数,不足2位则在开头加'0'
const m = (dt.getMonth()+ 1 + '').padStart(2,'0')
const d =(dt.getDate() + '').padStart(2,'0')
const hh =(dt.getHours() + '').padStart(2,'0')
const mm =(dt.getMinutes() + '').padStart(2,'0')
const ss =(dt.getSeconds() + '').padStart(2,'0')
return `${y}-${m}-${d}-${hh}:${mm}:${ss}`
})
在相应的模板中使用过滤器
<el-table-column prop="add_time" label="创建时间" width="120">
<template slot-scope="scope">
//scope为接受的表格所有数据.row是该行数据.add_time为该行的毫秒数据
{{scope.row.add_time | dateFormat}}
</template>
</el-table-column>