Vue 中獲取當前時間并實時刷新的實現代碼
1. 實現代碼
<template> <div> {{nowDate}}{{nowWeek}}{{nowTime}} </div></template><script>export default { data(){ return { nowDate: '', // 當前日期 nowTime: '', // 當前時間 nowWeek: '' // 當前星期 } }, methods:{ dealWithTime(data) { // 獲取當前時間 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() { // 頁面加載完后顯示當前時間 this.dealWithTime(new Date()) // 定時刷新時間 this.timer = setInterval(()=> { this.dealWithTime(new Date()) // 修改數據date }, 500) }, destroyed() { if (this.timer) { // 注意在vue實例銷毀前,清除我們的定時器 clearInterval(this.timer); } }}</script><style lang='scss' scope></style>
2. 實現效果
總結
到此這篇關于Vue 中獲取當前時間并實時刷新的實現代碼的文章就介紹到這了,更多相關vue獲取當前時間實時刷新內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
1. pip已經安裝好第三方庫但pycharm中import時還是標紅的解決方案2. 關于Mysql-connector-java驅動版本問題總結3. CSS自定義滾動條樣式案例詳解4. 詳解CSS偽元素的妙用單標簽之美5. 將properties文件的配置設置為整個Web應用的全局變量實現方法6. Ajax實現表格中信息不刷新頁面進行更新數據7. HTML <!DOCTYPE> 標簽8. SpringBoot+Shiro+LayUI權限管理系統項目源碼9. ajax post下載flask文件流以及中文文件名問題10. msxml3.dll 錯誤 800c0019 系統錯誤:-2146697191解決方法
