1.安装dayjs插件:npm install dayjs --save

2.新建文件:src/utils/formatdate.js

import dayjs from "dayjs"
export function formattingDate(date, format) {
    format = format || "YYYY-MM-DD HH:mm:ss";
    return dayjs(date).format(format);
  }

3.在要使用的组件里引入使用

<div>{{nowDate}}</div>



<script>
  import { formattingDate} from "@/util/formatdate.js";
export default {
  name: "NursingHeader",
  data(){
    return{
      nowDate:formattingDate(new Date(),"YYYY-MM-DD HH:mm:ss"),
      timer:''
    }
  },
  created(){
    //定时器,获取本地时间
    this.timer = setInterval(()=>{
      this.nowDate =formattingDate(new Date(),"YYYY-MM-DD HH:mm:ss")
    },1000)
  },
  beforeDestroy(){
    //在vue实例销毁前,清除定时器
    if(this.timer){
      clearInterval(this.timer)
    }
  }
};
</script>

Logo

前往低代码交流专区

更多推荐