// 时间格式化export const parseTime=(time)=>{if(typeof time ==='string'){time= parseInt(time)}const format='{y}-{d}-{m} {h}:{i}:{s}'const date = new Date(time);const formatObj={y:date.getFullYear(),m:date.getMonth()+1,//月是从0开始算的d:date.getDate(),h:date.getHours(),i:date.getMinutes(),s:date.getSeconds()}console.log(formatObj);const strTime= format.replace(/{(y|m|d|h|i|s)+}/g,(result,key)=>{let value=formatObj[key]if(result.length>0 && value <10 ){value ='0'+value}return value})return strTime}
replace的使用:String.replace与String.format - 乐小天 - 博客园
前端引用:
<view >{{comments.creat_time |formatTime}}//comments.creat_time是formatTime的参数,//formatTime若想穿更多参数,可以{{comments.creat_time |formatTime(a,b)}}</view>import {parseTime} from'@/utils/index.js';filters:{formatTime(time){return parseTime(time)}},
filter的使用:
需求:有些数据注定需要转化之后才能给人看,但是又不想每次都反复写三目或很长的转化表达式
比如: 时间,金额,性别等
页面展示: