vue如何動態(tài)獲取當前時間
更新時間:2023年12月02日 14:52:26 作者:性野喜悲
這篇文章主要介紹了vue如何動態(tài)獲取當前時間問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
vue動態(tài)獲取當前時間
獲取當前時間:
<template> <div id="home"> <span class="deadline">截止{{ gettime }}</span> </div> </template> <script> export default { name: "Home", data() { return { gettime: "", //當前時間 }; }, methods: { getTime() { var _this = this; let yy = new Date().getFullYear(); var mm = new Date().getMonth() > 9 ? new Date().getMonth() + 1 : new Date().getMonth() == 9 ? new Date().getMonth() + 1 : '0' + (new Date().getMonth() + 1); var dd = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate(); let hh = new Date().getHours(); let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes(); let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds(); _this.gettime = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss; }, currentTime() { setInterval(this.getTime, 1000); }, }, mounted() { this.currentTime(); }, }; </script> <style scoped> </style>?
獲取當前日期:
<template> <div id="home"> <span class="deadline">當前日期{{ sendTime }}</span> </div> </template> <script> export default { name: "Home", data() { return { sendTime: "", //當前時間 }; }, mounted() { this.format(); }, methods: { format() { const nowDate = new Date(); const date = { year: nowDate.getFullYear(), month: nowDate.getMonth() + 1, date: nowDate.getDate(), } const newmonth = date.month > 9 ? date.month : '0' + date.month const day = date.date > 9 ? date.date : '0' + date.date this.sendTime = date.year + '.' + newmonth + '.' + day }, //獲取當前時間 }, }; </script> <style scoped> </style>
vue獲取當前時間并時時刷新
頁面顯示
<div><span>{{nowDate}}</span><span class="houertime">{{hourDate}}</span></div>
圖片上傳失?。。。。?,我是分開年月日和時分秒,給時分秒加樣式
2022/11/24 18:30:20
data中定義變量和定時器
在created中放一個定時器,每秒讀取當前時間一次,
在定時器之前先加載,否則會延遲一秒
data(){ return { nowDate: null, hourDate: null, nowtimer: '' } }, created() { this.gettime() // 如果不先調(diào)用一次的話,頁面顯示一秒后,時間才會顯示 this.nowtimer = setInterval(this.gettime, 1000); }, methods: { gettime() { this.nowDate = new Date().toLocaleString('zh', { year: 'numeric', month: 'numeric', day: 'numeric' }) this.hourDate = new Date().toLocaleString('zh', { hour: 'numeric', minute: 'numeric', second: 'numeric' }) } },
最后進行銷毀
beforeDestroy () { if (this.nowDate && this.hourDate) { clearInterval(this.nowDate, this.hourDate) // 在Vue實例銷毀前,清除定時器 } }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- vue怎樣獲取當前時間,并且傳遞給后端(不用注解)
- vue 使用moment獲取當前時間及一月前的時間
- Vue 中如何利用 new Date() 獲取當前時間
- 基于vue實現(xiàn)8小時帶刻度的時間軸根據(jù)當前時間實時定位功能
- vue中實現(xiàn)當前時間echarts圖表時間軸動態(tài)的數(shù)據(jù)(實例代碼)
- Vue 按照創(chuàng)建時間和當前時間顯示操作(剛剛,幾小時前,幾天前)
- Vue 中獲取當前時間并實時刷新的實現(xiàn)代碼
- Vue filter 過濾當前時間 實現(xiàn)實時更新效果
- vue 2.1.3 實時顯示當前時間,每秒更新的方法
- element-ui vue input輸入框自動獲取焦點聚焦方式
- Vue3新增時自動獲取當前時間的操作方法
相關文章
vue 百度地圖(vue-baidu-map)繪制方向箭頭折線實例代碼詳解
這篇文章主要介紹了vue 百度地圖(vue-baidu-map)繪制方向箭頭折線,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04vue下載excel的實現(xiàn)代碼后臺用post方法
這篇文章主要介紹了vue下載excel的實現(xiàn)代碼,后臺用post方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下2019-05-05