先创建好vue项目后,然后在main.js内添加:
Vue.filter('dateFormat', function(originVal){
const dt = new Date(originVal)
const y = dt.getFullYear()
// padStart 填充到多少位,填充内容为'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}`
})
在需要格式化时间的位置:
<template slot-scope="scope">
{{ scope.row.add_time | dateFormat}}
</template>
其中 scope.row.add_time 的为你需要被格式化的变量,中间的 | 要加上。