utc时间:<div id="id"></div> </br></br>
let d = new Date();
console.log(d.toUTCString());
function formatDate(t) {
return t <= 9 ? "0" + t : t;
}
function dateTiem() {
let date = new Date();
date.toUTCString();
let month = (date.getMonth() + 1).toString().padStart(2, "0");
let strDate = date.getDate().toString().padStart(2, "0");
document.getElementById(
"id"
).innerText = `${date.getFullYear()} - ${month} - ${strDate} ${date.getHours()} : ${this.formatDate(
date.getMinutes()
)} : ${this.formatDate(date.getSeconds())}`;
setTimeout(() => {
this.dateTiem();
}, 1000);
}
this.dateTiem();
使用
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- import CSS -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
utc时间:<div>{{time}}</div>
</div>
</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
new Vue({
el: '#app',
data: function () {
return {
time: null,
}
},
created() {
setInterval(() => {
// 假设定时调用接口获取时间
console.log('定时');
this.getDate();
}, 5000);
setInterval(() => {
if(this.time) {
this.dateTiem()
}
}, 1000);
},
methods: {
getDate() {
this.time = '2020-11-25 11:11:11';
},
formatDate(t) {
return t <= 9 ? "0" + t : t;
},
dateTiem() {
let d = (new Date(this.time).getTime()) + 1000;
let date = new Date(d);
console.log(date);
let month = (date.getMonth() + 1).toString();
let strDate = date.getDate().toString().padStart(2, "0");
this.time = `${date.getFullYear()}-${month}-${strDate} ${date.getHours()}:${this.formatDate(
date.getMinutes()
)}:${this.formatDate(date.getSeconds())}`;
}
},
})
</script>
</html>