VUE环境下获取当前时间并格式化--按秒数更新

<el-col :span="8"><div class="grid-content title-time"> {{date}}</div></el-col>

  

mounted(){
    let _this = this; // 声明一个变量指向Vue实例this,保证作用域一致
    this.timer = setInterval(() => {
      _this.date =this.getTime(); // 修改数据date
    }, 1000)
  },
  methods:{
     getTime(){
       var date1=new Date();
       var year=date1.getFullYear();
       var month=date1.getMonth()+1;
       var day=date1.getDate();
       var hours=date1.getHours();
       var minutes=date1.getMinutes();
       var seconds=date1.getSeconds();
       return year+"年"+month+"月"+day+"日"+hours+":"+minutes+":"+seconds
     }
  },
  beforeDestroy() {
    if (this.timer) {
      clearInterval(this.timer); // 在Vue实例销毁前,清除我们的定时器
    }
  }

  

原文地址:https://www.cnblogs.com/JiAyInNnNn/p/11511436.html