在Js中试了其他的toxxxString方法,就这个toISOString()
方法的返回结果是正确的
index.js
Page({
data: {
},
onLoad() {
var time = new Date(2021, 3, 7, 12, 43, 0).toISOString();
this.setData({
time: time
});
}
})
index.wxml
<wxs src="tool.wxs" module="tool"/>
<view>
{{tool.timeFormat(time)}}
</view>
index.wxs
var timeFormat = function (time) {
var lastDate = getDate(time);
console.log(lastDate);
var lastTime = lastDate.getTime() / 1000;
var nowDate = getDate();
var nowTime = nowDate.getTime() / 1000;
var timeStamp = nowTime - lastTime;
var timeStr = "";
if (timeStamp < 60) {
timeStr = "刚刚";
} else if (timeStamp >= 60 && timeStamp < 60 * 60) {
var minutes = parseInt(timeStamp / 60);
timeStr = minutes + "分钟前";
} else if (timeStamp >= 60 * 60 && timeStamp < 60 * 60 * 24) {
var hours = parseInt(timeStamp / 60 / 60);
timeStr = hours + "小时前";
} else if (timeStamp >= 60 * 60 * 24 && timeStamp < 60 * 60 * 24 * 30) {
var days = parseInt(timeStamp / 60 / 60 / 24);
timeStr = days + "天前";
} else {
var year = lastDate.getFullYear();
var month = lastDate.getMonth();
var day = lastDate.getDay();
var hour = lastDate.getHours();
var minute = lastDate.getMinutes();
timeStr = year + '/' + month + '/' + day + ' ' + hour + ':' + minute;
}
return timeStr;
};
module.exports = {
timeFormat: timeFormat
};