Home > Web Front-end > Vue.js > How to display time in vue.JS

How to display time in vue.JS

coldplay.xixi
Release: 2020-11-12 16:24:00
Original
2541 people have browsed it

Vue.JS method of displaying time: first define a variable in data to store the time, the code is [data(){return {nowTime:''}}]; then given a div, the code is [<div>{{nowTime}}</div>].

How to display time in vue.JS

【Recommended related articles: vue.js

Vue.JS method of displaying time:

1. Define a variable in data to store the time

data(){
   return {
     nowTime:&#39;&#39;
   }
  },
Copy after login

2. Given a div

<div>{{nowTime}}</div>
Copy after login

3.js part

 //显示当前时间(年月日时分秒)
    timeFormate(timeStamp) {
      let year = new Date(timeStamp).getFullYear();
      let month =new Date(timeStamp).getMonth() + 1 < 10? "0" + (new Date(timeStamp).getMonth() + 1): new Date(timeStamp).getMonth() + 1;
      let date =new Date(timeStamp).getDate() < 10? "0" + new Date(timeStamp).getDate(): new Date(timeStamp).getDate();
      let hh =new Date(timeStamp).getHours() < 10? "0" + new Date(timeStamp).getHours(): new Date(timeStamp).getHours();
      let mm =new Date(timeStamp).getMinutes() < 10? "0" + new Date(timeStamp).getMinutes(): new Date(timeStamp).getMinutes();
      let ss =new Date(timeStamp).getSeconds() < 10? "0" + new Date(timeStamp).getSeconds(): new Date(timeStamp).getSeconds();
      this.nowTime = year + "年" + month + "月" + date +"日"+" "+hh+":"+mm+&#39;:&#39;+ss ;
    },
    nowTimes(){
      this.timeFormate(new Date());
      setInterval(this.nowTimes,1000);
      this.clear()
    },
    clear(){
      clearInterval(this.nowTimes)
      this.nowTimes = null;
    }
Copy after login

4. Just copy and paste it. Mainly use a timer, call it every second, and finally clear the timer and clear function

Related free learning recommendations:JavaScript(Video)

The above is the detailed content of How to display time in vue.JS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template