Vue如何獲取new Date().getTime()時間戳
更新時間:2024年10月16日 08:48:07 作者:qianer0_0
在Web開發(fā)中,前端使用Vue.js獲取的是毫秒級時間戳,而PHP后端則是秒級時間戳,處理此類問題時,需要將PHP的時間戳乘以1000轉(zhuǎn)換為毫秒級,以保證數(shù)據(jù)的一致性和正確的邏輯判斷
vue獲取new Date().getTime() 時間戳
在處理按鈕顯示的時候發(fā)現(xiàn)一個問題:
vue 通過new Date().getTime()獲取時間戳返回的是13位數(shù)字,單位是毫秒;
php后臺time()獲取的時間戳是10位數(shù)字,單位秒;
所以在判斷比較時需要將time()*1000 轉(zhuǎn)換為毫秒再去比較
<el-button v-if="new Date(scope.row.end_time*1000).getTime()>new Date().getTime()" size="mini" icon="edit" @click="editGroupsAction(scope.$index, scope.row)">編輯</el-button>
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>總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue計算屬性computed--getter和setter用法
這篇文章主要介紹了vue計算屬性computed--getter和setter用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
vue路由傳參方式的方式總結(jié)及獲取參數(shù)詳解
vue 路由傳參的使用場景一般都是應用在父路由跳轉(zhuǎn)到子路由時,攜帶參數(shù)跳轉(zhuǎn),下面這篇文章主要給大家介紹了關于vue路由傳參方式的方式總結(jié)及獲取參數(shù)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-07-07

