数字 日期 格式化方法
/** * Created By xuzhaohui on 2017/2/10 * * Vue全局过滤器 * */ import Vue from 'vue'; /** * 数字格式化,超过亿以亿为单位(保留1位小数点),超过万以万为单位(保留1位小数点),其它正常显示 */ Vue.filter('numUnit', (value) => { let num = parseInt(value); if(num>100000000) num = (Math.ceil(num/1000000)/100).toFixed(1) + '亿'; else if(num>10000){ num = (Math.ceil(num/100)/100).toFixed(1) + '万'; }else{ num = num; } return num; }); /** * 浏览量格式化 * 以万或亿为单位显示浏览量,小数点后精确两位 * 例如:1.21万 */ Vue.filter('viewNumber',(value) => { let num = parseInt(value); if(num>100000000) return Math.ceil(num/1000000)/100 + '亿'; else if(num>10000){ return Math.ceil(num/100)/100 + '万'; }else{ return num; } }); /** * 时间戳转化例如:12:23 * 时间秒转化时+分 */ Vue.filter('secondFormat', (value) => { let date = new Date(value), h = date.getHours(), s = date.getMinutes(); h = h<10?'0'+h.toString():h; s = s<10?'0'+s.toString():s; return h+':'+s }); /** * 显示时间过滤器:秒数转化时分秒显示 * @DateTime 2016-12-14 */ Vue.filter('show-time', (value) => { let h = Math.floor(value/3600)%24, m = Math.floor(value/60)%60, s = Math.floor(value)%60; h = h<10 ?'0'+h:h; m = m<10 ?'0'+m:m; s = s<10 ?'0'+s:s; return h+':'+m+':'+s; }); /** * 过去时间过滤器:当时间格式为“时间戳”时 * @DateTime 2017-04-24 */ Vue.filter('pass-timestamp', (value) => { let now = new Date().getTime(), diffValue = now - value; let month = diffValue / (1000*60*60*24*7*30), week = diffValue / (1000*60*60*24*7), date = diffValue / (1000*60*60*24), hour = diffValue / (1000*60*60), min = diffValue / (1000*60); if (month >= 1) { return parseInt(month) + "个月前"; } else if (week >= 1) { return parseInt(week) + "周前"; } else if (date >= 1) { return parseInt(date) + "天前"; } else if (hour >= 1) { return parseInt(hour) + "小时前"; } else if (min >= 1) { return parseInt(min) + "分钟前"; } else return "刚刚发表"; }); /** * 时间过滤器:当时间格式为“时间戳”时转化为例:2017-01-01 * @DateTime 2017-05-09 */ Vue.filter('show-date', (value) => { let date = new Date(value), year = date.getFullYear(), month= date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1, data = date.getDate() < 10 ? '0'+date.getDate() : date.getDate(); return year+'-'+month+'-'+data; }); /** * 编码转换 * 服务器端传过来的富文本数据格式需要转换 */ Vue.filter('decode',(value) => { let re = value+''; let exp = { '&':'&', '<':'<', '>':'>', '"':'"', ' ':' ', '“':'“', '”':'”' }; for(var item in exp) re = re.replace(new RegExp(item,'g'),exp[item]); return re; }); /*过去时间过滤器,当前时间格式为"2017-01-01"*/ Vue.filter('time-format',(value) => { let now = new Date().getTime(), oldTime = new Date(value.replace(new RegExp("-","gm"),"/")).getTime(), difference = now - oldTime, result = '', _year = difference/(1000*60*60*24*30*12), _month =difference/(1000*60*60*24*30), _week =difference/(1000*60*60*24*7), _day =difference/(1000*60*60*24), _hour =difference/(1000*60*60), _min =difference/(1000*60); if (_year>=1) { return parseInt(_year) + "年前" } else if(_month>=1) { return parseInt(_month) + "个月前" } else if(_week>=1) { return parseInt(_week) + "周前" } else if(_day>=1) { return parseInt(_day) +" 天前" } else if(_hour>=1) { return parseInt(_hour) +"小时前" } else if(_min>=1) { return parseInt(_min) +"分钟前" } else { return "刚刚发表" } })
请爱好前端技术的朋友,联系我,有问题大家一起讨论