在vue2页面中获取日期时间
<template>
<span>
{{ present.time }} {{ present.date }} {{ present.day }}
</span>
</template>
export default {
data() {
return {
present: { day: "", time: "", date: "", timer: null },
}
}
mounted() {
// 启动定时器
this.present.timer = setInterval(() => {
this.atRresent();
}, 1000);
},
// 删除定时器
destroyed() {
clearInterval(this.present.timer);
this.present.timer = null;
},
methods: {
atRresent() {
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const sun = date.getDate();
this.present.day = new Array(
"星期日",
"星期一",
"星期二",
"星期三",
"星期四",
"星期五",
"星期六"
)[date.getDay()];
const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
const second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
this.present.date = `${year}年${month}月${sun}日`;
this.present.time = `${hour}:${minute}:${second}`;
},
}
}
持续更新中...