大屏实时时间

1,014 阅读1分钟

//大屏实时时间

methods\index.js
Vue.prototype.getCurrentDayChsStr = function (format = "YYYY年MM月DD日") {
      // 将 gmt 的时间进行格式化
      // 获取当前日期
      // @params format
      // YYYY年MM月DD日 今天的日期
      // W周 当前星期几
      let date = new Date();
      let YYYY = this.$moment(date).format("YYYY");
      let MM = this.$moment(date).format("MM");
      let DD = this.$moment(date).format("DD");
      let WW = Number(this.$moment(date).format("d"));
      let WWArr = [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
      ];
      let getCurrentDayChsStr = `${YYYY}${MM}${DD}日`;
      if (format == "W周") {
        getCurrentDayChsStr = `${WWArr[WW]}`;
      }
      return getCurrentDayChsStr;
    };
    data(){
        dateValue: "",
        dateTime: "",
        weekValue: "",
        timer: null,
    }
    mounted() {
        this.getData(); //获取日期
        this.getTime(); // 获取时间
        this.timer = setInterval(() => {
            this.getTime();
        }, 1000);   //实时请求
 
    }
    methods: {
   //获取日期和星期几
     getData() {
            let myDate = new Date();
            let year = myDate.getFullYear();
            let month = myDate.getMonth() + 1;
            let day = myDate.getDate();
            this.dateValue = year + "年" + month + "月" + day + "日"; //日期
            this.weekValue = this.getCurrentDayChsStr("W周"); //星期几
        },
   //获取时间
     getTime() {
            let myDate = new Date();
            let hour = myDate.getHours();
            hour = hour >= 10 ? hour : "0" + hour;
            let minutes = myDate.getMinutes();
            minutes = minutes >= 10 ? minutes : "0" + minutes;
            let seconds = myDate.getSeconds();
            seconds = seconds >= 10 ? seconds : "0" + seconds;
            this.dateTime = hour + ":" + minutes + ":" + seconds + "";
        }, 
    //初始化
      clearTimer() {
            if (this.timer) {
                clearInterval(this.timer);
                this.timer = null;
            }
            this.timer = setInterval(() => {
                this.getTime();
            }, 1000);
       },
        
  
  }