欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue 中獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新的實(shí)現(xiàn)代碼

 更新時(shí)間:2020年05月12日 14:11:40   作者:明天也要努力  
這篇文章主要介紹了Vue 中獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

1. 實(shí)現(xiàn)代碼

<template>
 <div>
  {{nowDate}}{{nowWeek}}{{nowTime}}
 </div>
</template>

<script>
export default {
 data(){
  return {
   nowDate: "",  // 當(dāng)前日期
   nowTime: "",  // 當(dāng)前時(shí)間
   nowWeek: ""   // 當(dāng)前星期
  }
 },
 methods:{
  dealWithTime(data) { // 獲取當(dāng)前時(shí)間
   let formatDateTime;
   let Y = data.getFullYear();
   let M = data.getMonth() + 1;
   let D = data.getDate();
   let H = data.getHours();
   let Min = data.getMinutes();
   let S = data.getSeconds();
   let W = data.getDay();
   H = H < 10 ? "0" + H : H;
   Min = Min < 10 ? "0" + Min : Min;
   S = S < 10 ? "0" + S : S;
   switch (W) {
    case 0:
     W = "日";
     break;
    case 1:
     W = "一";
     break;
    case 2:
     W = "二";
     break;
    case 3:
     W = "三";
     break;
    case 4:
     W = "四";
     break;
    case 5:
     W = "五";
     break;
    case 6:
     W = "六";
     break;
    default:
     break;
   }
   this.nowDate = Y + "年" + M + "月" + D + "日 ";
   this.nowWeek = "周" + W ; 
   this.nowTime = H + ":" + Min + ":" + S;
   // formatDateTime = Y + "年" + M + "月" + D + "日 " + " 周" +W + H + ":" + Min + ":" + S;
  },
 },
 mounted() { 
  // 頁面加載完后顯示當(dāng)前時(shí)間
  this.dealWithTime(new Date())
  // 定時(shí)刷新時(shí)間
  this.timer = setInterval(()=> {
    this.dealWithTime(new Date()) // 修改數(shù)據(jù)date
  }, 500)
 }, 
 destroyed() {
  if (this.timer) { // 注意在vue實(shí)例銷毀前,清除我們的定時(shí)器
   clearInterval(this.timer);
  }
 }
}
</script>

<style lang="scss" scope>

</style>

2. 實(shí)現(xiàn)效果

在這里插入圖片描述

總結(jié)

到此這篇關(guān)于Vue 中獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)vue獲取當(dāng)前時(shí)間實(shí)時(shí)刷新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論