{{nowTime}}"; 3. Through "timeFormate(timeStamp) {... }" method to display the current date."/> {{nowTime}}"; 3. Through "timeFormate(timeStamp) {... }" method to display the current date.">

Home  >  Article  >  Web Front-end  >  How to implement date on the page in vuejs

How to implement date on the page in vuejs

藏色散人
藏色散人Original
2021-09-26 14:49:103258browse

Vuejs method to implement date on the page: 1. Define a variable in data to store the time; 2. Set "dc6dce4a544fdca2df29d5ac0ea9906b{{nowTime}}16b28748ea4df4d9c2150843fecfba68"; 3. Pass " timeFormate(timeStamp) {...}" method can display the current date.

How to implement date on the page in vuejs

The operating environment of this article: Windows 7 system, vue version 2.9.6, DELL G3 computer.

How does vuejs implement date on the page?

Display real-time time (year, month, day, hour, minute and second) in the vue project

1. Define a variable in data to store the time

data(){
   return {
     nowTime:''
   }
  },

2. Given a div

<div>{{nowTime}}</div>

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;
    }

4. Just copy and paste it, mainly use the timer, call it every second, and finally clear the timer, clear Function

Recommended: "The latest 5 vue.js video tutorial selections"

The above is the detailed content of How to implement date on the page in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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