两种办法
第一种 全局挂载 main.js
Vue.filter('dateFormat', function(originVal) {
const dt = new Date(originVal)
const y = dt.getFullYear()
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}`
})
html里面
<template slot-scope="scope">
{{scope.row.add_time | dateFormat}}
</template>
第二种办法
<td>{{item.add_time*1000 | timeFormat}}</td>
// 时间戳转换
function timestampToTime(timestamp) {
let date = new Date(timestamp)
var YY = date.getFullYear() + '-'
var MM =
(date.getMonth() + 1 < 10
? '0' + (date.getMonth() + 1)
: date.getMonth() + 1) + '-'
var DD = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
var hh =
(date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
var mm =
(date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) +
':'
var ss =
date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
return YY + MM + DD + ' ' + hh + mm + ss
}
filters: {
timeFormat: function (value) {
return timestampToTime(value);
}
},