Vue如何获取new Date().getTime()时间戳
作者:qianer0_0
在Web开发中,前端使用Vue.js获取的是毫秒级时间戳,而PHP后端则是秒级时间戳,处理此类问题时,需要将PHP的时间戳乘以1000转换为毫秒级,以保证数据的一致性和正确的逻辑判断
vue获取new Date().getTime() 时间戳
在处理按钮显示的时候发现一个问题:
vue 通过new Date().getTime()获取时间戳返回的是13位数字,单位是毫秒;
php后台time()获取的时间戳是10位数字,单位秒;
所以在判断比较时需要将time()*1000 转换为毫秒再去比较
<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动态获取当前时间
获取当前时间:
<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>总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
