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)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Jsp中request的3個(gè)基礎(chǔ)實(shí)踐2. jsp EL表達(dá)式詳解3. XML入門的常見問題(一)4. Django ORM實(shí)現(xiàn)按天獲取數(shù)據(jù)去重求和例子5. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實(shí)現(xiàn)6. Python多線程操作之互斥鎖、遞歸鎖、信號(hào)量、事件實(shí)例詳解7. idea設(shè)置自動(dòng)導(dǎo)入依賴的方法步驟8. idea給項(xiàng)目打war包的方法步驟9. Django程序的優(yōu)化技巧10. 怎樣才能用js生成xmldom對(duì)象,并且在firefox中也實(shí)現(xiàn)xml數(shù)據(jù)島?
