我是靠谱客的博主 甜美裙子,这篇文章主要介绍Vue js实时显示当前时间,现在分享给大家,希望可以做个参考。

当前实例在vue项目中

export default {
    data(){
        return{
            nowTimes:{
                yy:0,
                dd:"00",
                hou:"00",
                min:"00",
                sec:"00"
            },
            setTime:null
        }
    },
    mounted(){
        this.setTime = setInterval( ()=>{
            this.setNowTimes();
        },1000)
    },
    methods:{
        setNowTimes(){
            let myDate = new Date();
            this.nowTimes.yy = myDate.getMonth()+1;
            this.nowTimes.dd = String(myDate.getDate()<10?'0'+ myDate.getDate(): myDate.getDate()) ;
            this.nowTimes.hou = String(myDate.getHours()<10?"0"+myDate.getHours():myDate.getHours());
            this.nowTimes.min = String(myDate.getMinutes()<10?'0'+myDate.getMinutes():myDate.getMinutes());
            this.nowTimes.sec = String(myDate.getSeconds()<10?'0'+myDate.getSeconds():myDate.getSeconds());
        }
    },
    beforeUpdate(){
      clearInterval(this.setTime)
  }
}

vue项目就直接用吧!

最后

以上就是甜美裙子最近收集整理的关于Vue js实时显示当前时间的全部内容,更多相关Vue内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(34)

评论列表共有 0 条评论

立即
投稿
返回
顶部