vue get the current real-time refresh

Requirements: Get the current system time, year, month, day, hour show on the page, and refreshed in real time, and system time consistent 

Step 1: deta which declares two variables
Step two: the time to call in writing created () Life inside cycle, enter the page you need to call the
third step: leaving the page using beforeDestroy () destroy
as follows:
Data () {
     return { 
      Timer: "", // define a timer variable 
      currentTime: new new a Date (), // get the current time 
    }; 
  }, 
Created () { 
    var _this = the this ; // declare a variable to point examples Vue this, to ensure consistency scope 
    the this .timer = the setInterval ( function () { 
      _this.currentTime = // modify data DATE 
        new new a Date () the getFullYear () +. 
        "-" + 
        ( new new . a Date () The getMonth () + . 1) + 
        "-" + new new . a Date () getDate () + 
        "" +
        
        new new a Date () getHours () +. 
        ":" + new new a Date () getMinutes () +. 
        ":" + new new . a Date () the getSeconds (); 
    }, 1000 ); 
  }, 
beforeDestroy () { IF ( the this . the timer) { 
      clearInterval ( the this .timer); // before the destruction Vue example, we remove the timer     } 
  }
        
        
    

So you can get to meet the needs of the time format is 2019-8-168: 9: 5  

10 is not less than plus 0 

If necessary you can use the following method can be a plus

//过滤加0
appendZero(obj) {
if (obj < 10) {
return "0" + obj;
} else {
return obj;
}
},

 

Guess you like

Origin www.cnblogs.com/lidonglin/p/11362384.html