<template> <div> <h3>格式化时间</h3> <p>{{time|timeFormat}}</p> <p>{{time|timeFormat("YYYY")}}</p> </div> </template> <script> export default{ name:"foemt", data(){ return { time:new Date(), } }, mounted(){ setInterval(()=>{ this.time=new Date() },1000); }, } </script> <style scoped> </style>
注意:要如果没安装插件记得在项目安装插件:npm i moment --save
Vue.filter('timeFormat',(value,format='YYYY-MM-DD HH:mm:ss')=>{ return moment(value).format(format); });