前端实时显示日期周时间

![效果](https://img-blog.img.cn/df0de834ff844d0c885560406365d0e2.png#pic_center)

<div class="time" v-html="allDate"></div>
data() {
        return {
            time: null,
            allDate: 
        }
    },
    methods: {
        timeChange: function () {
            var that = this;
            this.time = setInterval(function () {
                var time = new Date();
                let year = time.getFullYear(); //获取年份
                let month = time.getMonth() + 1; //获取月份
                let day = time.getDate(); //获取日期
                let hour = checkTime(time.getHours()); //获取时
                let minite = checkTime(time.getMinutes()); //获取分
                let second = checkTime(time.getSeconds()); //获取秒企鹅330586621交流
                 let today = new Array(星期日, 星期一, 星期二, 星期三, 星期四, 星期五, 星期六);
                 let week = today[time.getDay()];
                /****当时、分、秒、小于10时,则添加0****/
                function checkTime(i) {
                    if (i < 10) return "0" + i;
                    return i;
                }
                that.allDate = `<span>week </span><span style="opacity: 0.4;">${year}-${month}-${day}</span><span style="opacity: 0.4;">${hour}:${minite}:${second}</span>`;
            }, 1000); //setInterval(fn,i) 定时器,每隔i秒执行fn

        },
        destroyed() {
        if (this.time) {
            clearInterval(this.time)
        }
    },
    created() {
        this.timeChange();
    },     
    },
经验分享 程序员 微信小程序 职场和发展