vue显示时间,时间戳格式化
``<template><div class="main"><!--當前時間--><div class="time" v-html="$options.fi
``
<template>
<div class="main">
<!--當前時間-->
<div class="time" v-html="$options.filters.formatDate(date)"></div>
</div>
</template>
<script>
export default {
name: "homePage",
data(){
return{
date:new Date()
}
},
mounted () {
var _this = this;
this.timer = setInterval(function() {
_this.date = new Date();
}, 1000);
},
beforeDestroy () {
if(this.timer) {
clearInterval(this.timer);
}
},
filters: {
formatDate: function (value) {
let date = new Date(value);
let y = date.getFullYear();
let MM = date.getMonth() + 1;
MM = MM < 10 ? ('0' + MM) : MM;
let d = date.getDate();
d = d < 10 ? ('0' + d) : d;
let h = date.getHours();
h = h < 10 ? ('0' + h) : h;
let m = date.getMinutes();
m = m < 10 ? ('0' + m) : m;
let s = date.getSeconds();
s = s < 10 ? ('0' + s) : s;
return y + '<span style="color: #fff;margin:0 10px">年</span>' + MM + '<span style="color: #fff;margin:0 10px">月</span>' + d + '<span style="color: #fff;margin:0 26px 0 10px">日</span> ' + h + ':' + m + ':' + s;
}
}
}
</script>
更多推荐
所有评论(0)