vue 时间过滤器
// <p>{{1589923213014| dateFormat('yyyy-MM-dd hh:mm:ss')}}</p> Vue.filter("dateFormat", function(value, format) { if (!value) return ''; let getDate; let dateY, dateH; if (typeof value !== Number) { dateY = value.split(/-|\/|T|\s/); let date = new Date(); dateH = dateY[dateY.length - 1].split(/:/); date.setUTCFullYear(dateY[0], dateY[1] - 1, dateY[2]); date.setUTCHours(dateH[0], dateH[1], dateH[2].substring(0, 2), 0); value = date; } getDate = new Date(value); let fmt; let obj = { 'M+': getDate.getMonth() + 1, 'd+': getDate.getDate(), 'h+': getDate.getHours(), 'm+': getDate.getMinutes(), 's+': getDate.getSeconds(), 'q+': Math.floor((getDate.getMonth() + 3) / 3), 'S': getDate.getMilliseconds() }; if (/(y+)/.test(format)) { // RegExp.$1...$9属性的值为String类型,返回上一次正则表达式匹配中,第n个子表达式所匹配的文本。此属性只保存最前面的9个匹配文本。 fmt = format.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length)); } fmt = fmt ? fmt : format; for (let k in obj) { if (new RegExp('(' + k + ')').test(fmt)) { fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (obj[k]) : (('00' + obj[k]).substr(('' + obj[k]).length))); } } return fmt; })
格式化数字, 用‘ , ’ 分开来;
Vue.filter("formatNumber", function(value, format) { let intg = value + ''; let dec = ''; let result = []; if (value.indexOf(".") > -1) { arr = value.split("."); intg = arr[0]; dec = "." + arr[1]; } intg.split(""); let numList = intg.split(""); let length = numList.length - 1; for (let i = length; i >= 0; i--) { result.unshift(numList[i]); if ((length - i + 1) % 3 === 0 && i != 0 && length - i != 0) { result.unshift(","); } } dec != "" ? result.push(dec) : ""; return result.join(""); })