华域联盟 JAVA vue实现右上角时间显示实时刷新

vue实现右上角时间显示实时刷新

本文实例为大家分享了vue实现右上角时间显示实时刷新的具体代码,供大家参考,具体内容如下

效果图

utils文件夹下的index.js

export default {
  // 获取右上角的时间戳
  formatDate(time) {
    let  newTime = "";
    let date = new Date(time);
    let a = new Array("日","一","二","三","四","五","六");
    let year = date.getFullYear(),
        month = date.getMonth()+1,//月份是从0开始
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds(),
        week = new Date().getDay();
        if(hour<10){
          hour = "0"+hour;
        }
        if(min<10){
          min="0"+min;
        }
        if(sec<10){
          sec = "0"+sec;
        }
    newTime = year + "-"+month+"-" +day +"  星期"+a[week] + " "+hour+":"+min+":"+sec;
    return newTime;
  }
}

src==>page文件夹下cs.vue

<template>
  <div class="main">   
    <!-- 头部 -->
    <div class="header">     
      <div class="cue_time">{{currentDate}}</div>
    </div>
  </div>
</template>
 
<script>
  import utils from '../utils/index' 
  export default {
    name:"tranin",
    data () {
      return{       
        currentDate: utils.formatDate(new Date()),
        currentDateTimer:null,//头部当前时间
      }
      
    },
    methods:{
      // 刷新头部时间
      refreashCurrentTime(){
        this.currentDate = utils.formatDate(new Date())
      }
 
    },
    mounted(){
      // 定时刷新时间
      this.currentDateTimer = setInterval(this.refreashCurrentTime,1000)
 
    }
  }
</script>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持华域联盟。

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » vue实现右上角时间显示实时刷新

转载请保留出处和原文链接:https://www.cnhackhy.com/83632.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部