vue如何動(dòng)態(tài)獲取當(dāng)前時(shí)間
vue動(dòng)態(tài)獲取當(dāng)前時(shí)間
獲取當(dāng)前時(shí)間:
<template>
<div id="home">
<span class="deadline">截止{{ gettime }}</span>
</div>
</template>
<script>
export default {
name: "Home",
data() {
return {
gettime: "", //當(dāng)前時(shí)間
};
},
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>?獲取當(dāng)前日期:
<template>
<div id="home">
<span class="deadline">當(dāng)前日期{{ sendTime }}</span>
</div>
</template>
<script>
export default {
name: "Home",
data() {
return {
sendTime: "", //當(dāng)前時(shí)間
};
},
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
}, //獲取當(dāng)前時(shí)間
},
};
</script>
<style scoped>
</style>vue獲取當(dāng)前時(shí)間并時(shí)時(shí)刷新
頁(yè)面顯示
<div><span>{{nowDate}}</span><span class="houertime">{{hourDate}}</span></div>圖片上傳失敗?。。?!,我是分開(kāi)年月日和時(shí)分秒,給時(shí)分秒加樣式
2022/11/24 18:30:20
data中定義變量和定時(shí)器
在created中放一個(gè)定時(shí)器,每秒讀取當(dāng)前時(shí)間一次,
在定時(shí)器之前先加載,否則會(huì)延遲一秒
data(){
return {
nowDate: null,
hourDate: null,
nowtimer: ''
}
},
created() {
this.gettime() // 如果不先調(diào)用一次的話,頁(yè)面顯示一秒后,時(shí)間才會(huì)顯示
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' })
}
},最后進(jìn)行銷(xiāo)毀
beforeDestroy () {
if (this.nowDate && this.hourDate) {
clearInterval(this.nowDate, this.hourDate) // 在Vue實(shí)例銷(xiāo)毀前,清除定時(shí)器
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 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 中獲取當(dāng)前時(shí)間并實(shí)時(shí)刷新的實(shí)現(xiàn)代碼
- Vue filter 過(guò)濾當(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項(xiàng)目中監(jiān)聽(tīng)手機(jī)物理返回鍵的實(shí)現(xiàn)
這篇文章主要介紹了vue項(xiàng)目中監(jiān)聽(tīng)手機(jī)物理返回鍵的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
Vue分別運(yùn)用class綁定和style綁定通過(guò)點(diǎn)擊實(shí)現(xiàn)樣式切換
這篇文章主要為大家介紹了Vue分別運(yùn)用class綁定和style綁定通過(guò)點(diǎn)擊實(shí)現(xiàn)樣式切換,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
vue中datepicker的使用教程實(shí)例代碼詳解
這篇文章主要介紹了vue-datepicker的使用,本文通過(guò)實(shí)例代碼大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-07-07
vue3基礎(chǔ)組件開(kāi)發(fā)detePicker日期選擇組件示例
這篇文章主要為大家介紹了vue3基礎(chǔ)組件開(kāi)發(fā)-detePicker(日期選擇組件)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
vue 實(shí)現(xiàn)強(qiáng)制類型轉(zhuǎn)換 數(shù)字類型轉(zhuǎn)為字符串
今天小編就為大家分享一篇vue 實(shí)現(xiàn)強(qiáng)制類型轉(zhuǎn)換 數(shù)字類型轉(zhuǎn)為字符串,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11
vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例
本文主要介紹了vue3實(shí)現(xiàn)H5表單驗(yàn)證組件的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04

