(尚015)Vue过滤器(对显示的数据进行格式化)

现在日期为:当前时间-1970年1月1日0时0分0秒的时间差

 

 日期格式化:百度搜索moment

 1.test015.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--需求;对当前时间进行指定格式显示-->
<div id="test">
<h2>显示格式化的日期时间</h2>
<p>{{date}}</p>
<p>完整版:{{date|dateString}}</p>
<p>年月日:{{date|dateString('YYYY-MM-DD')}}</p>
<p>时分秒:{{date|dateString('HH:mm:ss')}}</p>
</div>

<script type="text/javascript" src="../js/vue.js"></script>
<script type="text/javascript" src="https://cdn.bootcss.com/moment.js/2.24.0/moment.js"></script>
<script type="text/javascript">
//自定义过滤器
//Vue的函数对象(Vue现在是函数,但现在当对象使用)的filter()方法
//<p>完整版:{{date|dateString}}</p>将这个表达式的值传递给下面这个函数
  //format对应于上
<p>年月日:{{date|dateString('YYYY-MM-DD')}}</p>括号中有没有值
    Vue.filter('dateString',function(value,format){
return moment(value).format(format||'YYYY-MM-DD HH:mm:ss')
})

new Vue({
el:'#test',
data:{
date: new Date()
}
})
</script>
</body>
</html>
2.显示截图



原文地址:https://www.cnblogs.com/curedfisher/p/12023445.html