<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 --savehtml
Vue.filter('timeFormat',(value,format='YYYY-MM-DD HH:mm:ss')=>{ return moment(value).format(format); });