//定义全局的时间过滤器functionGetDate(originval){//需要处理的时间const dt=newDate(originval*1000)//年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,'')//分const mm =(dt.getMinutes()+'').padStart(2,'')//秒const ss =(dt.getSeconds()+'').padStart(2,'')//返回时间格式 yyyy-mm-dd hh:mm:ssreturn`${y}-${m}-${d}${hh}:${mm}:${ss}`}//应用于插值表达式中
Vue.filter('dateFormat',GetDate)//应用于方法中Vue.prototype.$getDate = GetDate
组件中使用
<template><div><el-row><el-col:span="4">在插值表达式中使用</el-col><el-col:span="4">{{dateTime | dateFormat}}</el-col></el-row><el-row><el-col:span="4">方法中获取使用</el-col><el-col:span="4">{{time}}</el-col></el-row></div></template><script>exportdefault{data(){return{dateTime:1588037605,time:0}},mounted(){this.getDate()},methods:{getDate(){const date =this.$getDate(this.dateTime)this.time = date
}}}</script><style></style>