Vue implements real-time refresh of the time display in the upper right corner

Vue implements real-time refresh of the time display in the upper right corner

This article example shares the specific code of Vue to achieve real-time refresh of the time display in the upper right corner for your reference. The specific content is as follows

Rendering

index.js in the utils folder

export default {
  // Get the timestamp in the upper right corner formatDate(time) {
    let newTime = "";
    let date = new Date(time);
    let a = new Array("日","MON","TUE","WED","THUR","FRI","SAT");
    let year = date.getFullYear(),
        month = date.getMonth()+1, //Month starts from 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 +" week"+a[week] + " "+hour+":"+min+":"+sec;
    return newTime;
  }
}

src==>cs.vue in the page folder

<template>
  <div class="main">   
    <!-- Header -->
    <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, //header current time}
      
    },
    methods:{
      // Refresh header time refreashCurrentTime(){
        this.currentDate = utils.formatDate(new Date())
      }
 
    },
    mounted(){
      // Timed refresh time this.currentDateTimer = setInterval(this.refreashCurrentTime,1000)
 
    }
  }
</script>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Implementation code for getting the current time and refreshing in real time in Vue
  • Vue2.0 countdown plug-in (timestamp refresh jump is not affected)

<<:  MySQL 8.0.23 installation and configuration method graphic tutorial under win10

>>:  Example of ellipsis when CSS multi-line text overflows

Recommend

Solve the splicing problem of deleting conditions in myBatis

I just learned mybatis today and did some simple ...

Detailed explanation of JavaScript implementation of hash table

Table of contents 1. Hash table principle 2. The ...

React implements dynamic pop-up window component

When we write some UI components, if we don't...

How to redirect URL using nginx rewrite

I often need to change nginx configuration at wor...

About dynamically adding routes based on user permissions in Vue

Display different menu pages according to the use...

Example of using CSS to achieve floating effect when mouse moves over card

principle Set a shadow on the element when hoveri...

Examples of using MySQL pessimistic locking and optimistic locking

Pessimistic Lock Pessimistic lock, considers the ...

WeChat applet realizes left-right linkage

This article shares the specific code for WeChat ...

An article to understand the usage of typeof in js

Table of contents Base Return Type String and Boo...

Detailed steps to install and uninstall Apache (httpd) service on centos 7

uninstall First, confirm whether it has been inst...

jQuery implements simple pop-up window effect

This article shares the specific code of jQuery t...

Complete steps for Docker to pull images

1. Docker pull pulls the image When using $ docke...

19 MySQL optimization methods in database management

After MySQL database optimization, not only can t...