1.新建time.wxs文件,并写入以下代码
/**
* 处理日期显示
*/
function getGapTime (date) { //传入的date为‘yyyy-mm-dd hh:mm:ss’形式的
var re = "00"
var timestamp = getDate().getTime();//wxs中不能使用js中的Date()函数,只能使用getDate()来获取当前标准时间,getTime()函数可以用来将标准时间转换为时间戳,单位为ms
timestamp = parseInt(timestamp / 1000);)
//发表文章的时间戳 s
var publicstamp = getDate(date).getTime();
publicstamp = parseInt(publicstamp / 1000)
//时间差s
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) //时间超过1个月返回具体的 年-月-日
}
return re
}
//将自定义函数暴露出来
//形式为 对外使用的函数名:wxs中定义的函数名
module.exports = {
getGap:getGapTime
}
2.在wxml中引入wxs文件
<wxs module = "aaa" src="../../wxs/time.wxs"></wxs>
3.调用wxs函数
<text class="date">{{aaa.getGap(myTime)}}</text>
版权声明:本文为weixin_41920883原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。