<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
</head>
<body>
<div id="ssl">
{{currentTime|filterTime}}
</div>
</body>
<script>
var em = new Vue({
el: "#ssl",
data: {
currentTime: new Date(), // 获取当前时间
},
filters: {
filterTime(val) {
var Y = val.getFullYear()
var M = val.getMonth()
var D = val.getDate()
var H = val.getHours()
var MI = val.getMinutes()
var S = val.getSeconds()
return Y + "年" + M + "月" + D + "日" + H + "时" + MI + "分" + S + "秒"
}
},
//声明周期函数 是最早使用data数据的函数
created() {
var _this = this; //声明一个变量指向Vue实例this,保证作用域一致
setInterval(function () {
_this.currentTime = new Date()//修改数据让他可以实时更新
}, 1000);
}
})
</script>
</html>
实现年月日转换
来自转载,亲测有效
filter过滤
最新推荐文章于 2024-11-13 11:52:56 发布