Vue 中獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新的實(shí)現(xiàn)代碼
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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue如何動(dòng)態(tài)獲取當(dāng)前時(shí)間
- vue怎樣獲取當(dāng)前時(shí)間,并且傳遞給后端(不用注解)
- vue 使用moment獲取當(dāng)前時(shí)間及一月前的時(shí)間
- Vue 中如何利用 new Date() 獲取當(dāng)前時(shí)間
- 基于vue實(shí)現(xiàn)8小時(shí)帶刻度的時(shí)間軸根據(jù)當(dāng)前時(shí)間實(shí)時(shí)定位功能
- vue中實(shí)現(xiàn)當(dāng)前時(shí)間echarts圖表時(shí)間軸動(dòng)態(tài)的數(shù)據(jù)(實(shí)例代碼)
- Vue 按照創(chuàng)建時(shí)間和當(dāng)前時(shí)間顯示操作(剛剛,幾小時(shí)前,幾天前)
- Vue filter 過濾當(dāng)前時(shí)間 實(shí)現(xiàn)實(shí)時(shí)更新效果
- vue 2.1.3 實(shí)時(shí)顯示當(dāng)前時(shí)間,每秒更新的方法
- element-ui vue input輸入框自動(dòng)獲取焦點(diǎn)聚焦方式
- Vue3新增時(shí)自動(dòng)獲取當(dāng)前時(shí)間的操作方法
相關(guān)文章
vue-iview動(dòng)態(tài)新增和刪除的方法
這篇文章主要為大家詳細(xì)介紹了vue-iview動(dòng)態(tài)新增和刪除的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06
antdesign-vue結(jié)合sortablejs實(shí)現(xiàn)兩個(gè)table相互拖拽排序功能
這篇文章主要介紹了antdesign-vue結(jié)合sortablejs實(shí)現(xiàn)兩個(gè)table相互拖拽排序功能,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn)
VuePress為每一個(gè)由它生成的頁面提供預(yù)加載的html,不僅加載速度極佳,同時(shí)對(duì)seo非常友好。這篇文章主要介紹了基于vue-ssr的靜態(tài)網(wǎng)站生成器VuePress 初體驗(yàn),需要的朋友可以參考下2018-04-04
vue使用動(dòng)態(tài)組件實(shí)現(xiàn)TAB切換效果
這篇文章主要介紹了vue使用動(dòng)態(tài)組件實(shí)現(xiàn)TAB切換效果的方法,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-05-05

