1.新建time.wxs文件,并写入以下代码
function getGapTime (date) {
var re = "00"
var timestamp = getDate().getTime();
timestamp = parseInt(timestamp / 1000);)
var publicstamp = getDate(date).getTime();
publicstamp = parseInt(publicstamp / 1000)
var gap = timestamp - publicstamp
if (gap < 60) {
re = "刚刚"
} else if (gap < 60 * 60) {
re = parseInt(gap / 60) + "分钟前"
} else if (gap < 60 * 60 * 24) {
re = parseInt(gap / 60 / 60) + "小时前"
} else if (gap < 60 * 60 * 24 * 30) {
re = parseInt(gap / 60 / 60 / 24) + "天前"
} else {
re = date.substring(0,10)
}
return re
}
module.exports = {
getGap:getGapTime
}
2.在wxml中引入wxs文件
<wxs module = "aaa" src="../../wxs/time.wxs"></wxs>
3.调用wxs函数
<text class="date">{{aaa.getGap(myTime)}}</text>