1、创建 filters.js 文件:
2、filters.js 中定义全局过滤方法:
export default {
/** 时间戳转换 */
showTime (time) {
let date = null
if ((time + '').length === 10) {
date = new Date(time * 1000)
} else {
date = new Date(time)
}
const Y = date.getFullYear()
const M = date.getMonth() + 1
const D = date.getDate()
const H = date.getHours()
const MM = date.getMinutes()
const S = date.getSeconds()
return `${Y}-${(M > 9 ? M : ('0' + M))}-${(D > 9 ? D : ('0' + D))} ${(H > 9 ? H : ('0' + H))}:${(MM > 9 ? MM : ('0' + MM))}:${(S > 9 ? S : ('0' + S))}`
},
/** 根据身份证号获取出生日期 */
getBirthday (idCard) {
let birthday = ''
if (idCard) {
if (idCard.length === 15) {
birthday = `19${idCard.substr(6, 6)}`
} else if (idCard.length === 18) {
birthday = idCard.substr(6, 8)
}
birthday = birthday.replace(/(.{4})(.{2})/, '$1-$2-')
}
return birthday
}
}
3、main.js入口文件引用
import filters from './filters' Object.keys(filters).forEach(k => {
Vue.filter(k, filters[k])
})
4、组建中使用
<template> <div>{{ 1454664434 | showTime }}</div> </template>
视图显示