vue过滤器格式化日志
2020-03-09 22:50
585 查看
vue过滤器格式化时间
有一个写好了的标准格式化方法,可以用过滤器直接调用
/** * 对日期进行格式化, * @param date 要格式化的日期 * @param format 进行格式化的模式字符串 * 支持的模式字母有: * y:年, * M:年中的月份(1-12), * d:月份中的天(1-31), * h:小时(0-23), * m:分(0-59), * s:秒(0-59), * S:毫秒(0-999), * q:季度(1-4) * @return String * @author yanis.wang * @see http://yaniswang.com/frontend/2013/02/16/dateformat-performance/ */ function dateFormat(date, format) { if (typeof date === "string") { var mts = date.match(/(\/Date\((\d+)\)\/)/); if (mts && mts.length >= 3) { date = parseInt(mts[2]); } } date = new Date(date); if (!date || date.toUTCString() == "Invalid Date") { return ""; } var map = { "M": date.getMonth() + 1, //月份 "d": date.getDate(), //日 "h": date.getHours(), //小时 "m": date.getMinutes(), //分 "s": date.getSeconds(), //秒 "q": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; format = format.replace(/([yMdhmsqS])+/g, function(all, t) { var v = map[t]; if (v !== undefined) { if (all.length > 1) { v = '0' + v; v = v.substr(v.length - 2); } return v; } else if (t === 'y') { return (date.getFullYear() + '').substr(4 - all.length); } return all; }); return format; }
具体过滤器实现如下:
<body> <div>标准时间函数格式化结果:{{date| format('yyyy-MM-dd')}}</div> <script> Vue.filter('format', function(value, arg) { ~~function dateFormat(date, format) { }~~//刚才的格式化函数可以直接写在这个位置 return dateFormat(value, arg); }) var vm=new Vue({ el: '#app', data: { date: new Date()//获取当前时间 } }) </script> </body>
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Vue自定义过滤器格式化数字三位加一逗号实现代码
- vue 自定义过滤器 格式化金额(保留两位小数)
- vue使用过滤器实现时间格式化
- Vue自定义过滤器格式化数字三位加一逗号
- vue过滤器--格式化时间
- vue中格式化时间过滤器代码实例
- js将number数值转化成为货币格式,货币格式化,金钱过滤器,货币过滤器,vue货币过滤金钱过滤全局和局部两种实现方式
- Vue教程09(过滤器应用-时间格式字符串格式化)
- Vue.js使用filter自定义过滤器对时间进行格式化(javascript原生写法)
- log4.net 配置 - 自定义过滤器按LoggerName过滤日志
- vue学习日志(1)
- vue学习十二----过滤器(采用 私有/公有对 new Date()时间格式转化)
- Vue过滤器filter
- gulp配合vue压缩代码格式化
- Vuejs——(2)Vue生命周期,数据,手动挂载,指令,过滤器
- ionic准备之angular基础——格式化数据以及过滤器(8)
- vue的过滤器,发送请求,路由,生命周期钩子的简单介绍
- VUE过滤器
- django 过滤器 、日期格式化参数
- 如何自定义一个vue.js的过滤器?