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)獲取當(dāng)前時間
獲取當(dāng)前時間:
<template> <div id="home"> <span class="deadline">截止{{ gettime }}</span> </div> </template> <script> export default { name: "Home", data() { return { gettime: "", //當(dāng)前時間 }; }, 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)前時間 }; }, 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)前時間 }, }; </script> <style scoped> </style>
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue實(shí)現(xiàn)簡易計(jì)算器的4種方法舉例
這篇文章主要給大家介紹了關(guān)于vue實(shí)現(xiàn)簡易計(jì)算器的4種方法,文中通過代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue覺有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09vue計(jì)算屬性computed--getter和setter用法
這篇文章主要介紹了vue計(jì)算屬性computed--getter和setter用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Vue實(shí)現(xiàn)Google第三方登錄的示例代碼
本文記錄作者在vue項(xiàng)目中使用到Google第三方登錄,查詢到的資料文檔也不詳細(xì),故此把自己所遇到的坑及問題詳細(xì)的記錄下來。2021-07-07vue路由傳參方式的方式總結(jié)及獲取參數(shù)詳解
vue 路由傳參的使用場景一般都是應(yīng)用在父路由跳轉(zhuǎn)到子路由時,攜帶參數(shù)跳轉(zhuǎn),下面這篇文章主要給大家介紹了關(guān)于vue路由傳參方式的方式總結(jié)及獲取參數(shù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07