<!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="./lib/vue.js"></script>
</head>
<body>
<div id="app">
<p>{{ currentDate|dateformat("11") }}</p>
</div>
<script>
// 定义一个过滤器,名字叫做dateformat,可以接受一个参数,判断参数要什么格式,如果是yyyy-mm-dd,就会显示年月日,否则显示具体时间.
Vue.filter("dateformat",function(datestr,patten=""){
dt = new Date(datestr);
y = dt.getFullYear();
m = dt.getMonth()+1
d = dt.getDate()
if(patten.toLocaleLowerCase === 'yyyy-mm-dd'){
return `${y}-${m}-${d}`
} else {
hh = dt.getHours()
mm = dt.getMinutes()
ss = dt.getSeconds()
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}
})
var vm=new Vue({
el:'#app',
data:{
currentDate: new Date()
},
methods:{}
});
</script>
</body>
</html>