0


VUE实时显示当前时间

    做大屏页面的时候要求在页面上加上当前时间,实现此功能的写法有很多种,我的源码如下,各位拿了直接用就行(根据自己的需求修改下样式):
<template>
  <div style="text-align: center;padding-top: 100px">
    当前时间:{{ nowTime }}
  </div>
</template>
<script>

export default {
  data() {
    return {
      nowTime: ''
    };
  },
  mounted() {
    this.getNowTime();

  },
  methods: {
    getNowTime () {
      let speed = 1000
      let that = this
      let theNowTime = function () {
        that.nowTime = that.timeNumber()
      }
      setInterval(theNowTime, speed)
    },
    timeNumber () {
      let today = new Date()
      let date = today.getFullYear() + '-' + this.twoDigits(today.getMonth() + 1) + '-' + this.twoDigits(today.getDate())
      let time = this.twoDigits(today.getHours()) + ':' + this.twoDigits(today.getMinutes()) + ':' + this.twoDigits(today.getSeconds())
      return date + '  ' + time
    },
    twoDigits (val) {
      if (val < 10) return '0' + val
      return val
    },
  },
}
</script>

页面效果:

标签: 前端 javascript html

本文转载自: https://blog.csdn.net/weixin_42258633/article/details/127982317
版权归原作者 DLoong+ 所有, 如有侵权,请联系我们删除。

“VUE实时显示当前时间”的评论:

还没有评论