获取当前时间,星期几

发布时间 2023-09-05 10:20:45作者: 小闫的姑娘

 

 

 

 

this.timer = setInterval(this.getTime, 1000);

 

 

//实时刷新时间
            getTime() {
                const date = new Date();
                const year = date.getFullYear();
                const month = date.getMonth() + 1;
                const day = date.getDate();
                const hour = date.getHours();
                const minute = date.getMinutes();
                const second = date.getSeconds();
                this.month = check(month);
                this.day = check(day);
                this.hour = check(hour);
                this.minute = check(minute);
                this.second = check(second);
            
                function check(i) {
                    const num = (i < 10) ? ("0" + i) : i;
                    return num;
                }
                this.nowDate = year + "" + this.month + "" + this.day + "";
                this.nowTime = this.hour + ":" + this.minute + ":" + this.second;
        this.time = ' 星期' + '日一二三四五六'.charAt(new Date().getDay())
            },
    beforeDestroy() {
            if (this.timer) {
                clearInterval(this.timer); // 在Vue实例销毁前,清除定时器
            }
        },