vue中动态显示时间

本文介绍了一个Vue组件,展示了如何使用JavaScript实现页面上实时更新的时间显示,并提供了日期时间格式化的函数,确保了小时、分钟等始终是两位数格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我也是参考别人的。

  • 代码如下
export default {
  name: 'Preview',
  data() {
    return {
      timer: undefined,
      nowTime: new Date(),
    };
  },
  created() {
    // 要显示时间,在渲染页面之前一直调用该函数,对this.time进行赋值开启定时
    this.timer = setInterval(() => {
      //时间格式1
      //this.nowTime = new Date().toLocaleString();
      
      //时间格式2
      this.nowTime = this.FormatTime();
    });
  },

  // 关闭页面销毁定时器
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer);
    }
  },

  methods: {
    FormatTime() {
        //设置返回显示的日期时间格式
        var date    = new Date();
        var year    = this.format2Digit(date.getFullYear());
        var month   = this.format2Digit(date.getMonth() + 1);
        var day     = this.format2Digit(date.getDate());
        var hour    = this.format2Digit(date.getHours());
        var minute  = this.format2Digit(date.getMinutes());
        var second  = this.format2Digit(date.getSeconds());
        var weekday = date.get
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳鲲鹏

能给阁下一点帮助,非常荣幸

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值