VUE3+Element plus 展示当前时间

template内容

<template>
        <div class="rightTop" style="display:inline-flex;align-items: center;flex-direction: row-reverse;height: 100%;">
            <button style="width:80px;height:30px;color:white;background-color: #8b0202;border-radius:5px; border:none; margin-right: 25px;">
                退出交易
            </button>
            <button style="width:80px;height:30px;color:white;background-color: #8b0202;border-radius:5px; border:none;margin-left: 25%; margin-right: 25px;">
                修改密码
            </button>
            <span class="hidden-md-and-down" style="font-weight: bold;white-space: nowrap;">服务器时间:&nbsp;&nbsp;{{currentTime}}</span>
        </div>
    </div>
</template>

script内容

export default {
        data() {
            return {
                timer: "",//定义一个定时器的变量
                currentTime: '', // 获取当前时间
            }
        },
        created() {
            this.timer = setInterval(() => {
                var year = new Date().getFullYear();
                var month = this.appendZero(new Date().getMonth() + 1);
                var day = this.appendZero(new Date().getDate());
                var hour = this.appendZero(new Date().getHours());
                var minute = this.appendZero(new Date().getMinutes());
                var second = this.appendZero(new Date().getSeconds());
                //修改数据date
                this.currentTime = year + "-" + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
            }, 1000);

        },
        methods: {
            //时间过滤加0
            appendZero(obj) {
                if (obj < 10) {
                    return "0" + obj;
                } else {
                    return obj;
                }
            },
        },
        beforeUnmount() {
            if (this.timer) {
                clearInterval(this.timer); // 在Vue实例销毁前,清除我们的定时器
            }
        }
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>