时间 实时更新

html

<p>{{count_time}}</p>

data数据

count_time: '', //格式化之后的当前时间

函数

      created() {
            this.getTimes()
        },
        beforeDestroy() {
            if (this.count_time) {
                clearInterval(this.getTimesInterval);
            }
        },
        methods: {
            // 时间实时更新
            getTimes() {
                setInterval(this.getTimesInterval, 100);
            },
            getTimesInterval: function () {
                let _this = this;
                let year = new Date().getFullYear(); //获取当前时间的年份
                let month = new Date().getMonth() + 1; //获取当前时间的月份
                let day = new Date().getDate(); //获取当前时间的天数
                let hours = new Date().getHours(); //获取当前时间的小时
                let minutes = new Date().getMinutes(); //获取当前时间的分数
                let seconds = new Date().getSeconds(); //获取当前时间的秒数
                //当小于 10 的是时候,在前面加 0
                if (hours < 10) {
                    hours = "0" + hours;
                }
                if (minutes < 10) {
                    minutes = "0" + minutes;
                }
                if (seconds < 10) {
                    seconds = "0" + seconds;
                }
                //拼接格式化当前时间
                _this.count_time = year + "\xa0-\xa0" + month + "\xa0-\xa0" + day + "\xa0\xa0\xa0" + hours + "\xa0:\xa0" + minutes + "\xa0:\xa0" +
                    seconds;
            },
        }
posted @ 2020-11-25 10:34  JaneLifeVlog  阅读(140)  评论(0)    收藏  举报