美文网首页
Vue项目中实时更新当前日期

Vue项目中实时更新当前日期

作者: 兰觅 | 来源:发表于2020-11-12 10:19 被阅读0次

    源码如下:

    <template>
      <div>时间:{{ nowDate }}</div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          nowDate: "", // 当前日期
        };
      },
      methods: {
        currentTime() {
          setInterval(this.formatDate, 500);
        },
        formatDate() {
          let date = new Date();
          let year = date.getFullYear(); // 年
          let month = date.getMonth() + 1; // 月
          let day = date.getDate(); // 日
          let week = date.getDay(); // 星期
          let weekArr = [ "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" ];
          let hour = date.getHours(); // 时
          hour = hour < 10 ? "0" + hour : hour; // 如果只有一位,则前面补零
          let minute = date.getMinutes(); // 分
          minute = minute < 10 ? "0" + minute : minute; // 如果只有一位,则前面补零
          let second = date.getSeconds(); // 秒
          second = second < 10 ? "0" + second : second; // 如果只有一位,则前面补零
          this.nowDate = `${year}/${month}/${day} ${hour}:${minute}:${second} ${weekArr[week]}`;
        }
      },
      mounted() {
        this.currentTime();
      },
      // 销毁定时器
      beforeDestroy() {
        if (this.formatDate) {
          clearInterval(this.formatDate); // 在Vue实例销毁前,清除时间定时器
        }
      }
    };
    </script>
    

    效果:


    效果图

    相关文章

      网友评论

          本文标题:Vue项目中实时更新当前日期

          本文链接:https://www.haomeiwen.com/subject/gvbubktx.html