filters:{
dateFilter (time) {
if (!time) { //当时间是null或者无效格式时返回空
return ' '
} else {
const timeLen = time.length; //传入的时候时间戳类型应为字符串,因为要根据length判断是10/13的时间戳
const oneDate = new Date(parseInt(time) * 1000); // 10位时间戳
const twoDate = new Date(parseInt(time)); // 13位时间戳
const date = timeLen == 10 ? oneDate : twoDate;
const dateNumFun = (num) => num < 10 ? `0${num}` : num
console.log(time)
const [Y, M, D, h, m, s] = [
date.getFullYear(),
dateNumFun(date.getMonth() + 1),
dateNumFun(date.getDate()),
dateNumFun(date.getHours()),
dateNumFun(date.getMinutes()),
]
return `${Y}-${M}-${D}`
}
}
},
用法:
<view class="orderTime">订单日期:{{1534675643 | dateFilter }}</view>