在学习如何获取云函数中真实数据中,其他所有的内容都能够显示出来,唯独时间显示不出来,在百度上查了很多,如果wxs中time不是日期类型,在JS中该如何转换?附上图片和代码如下
云函数代码:
if(errcode == 0){
return await db.collection("weibo").add({
data:{
content:content,
location:location,
author:author,
images:images,
video:video,
createTime:db.serverDate(),//插入到数据库的时间
device:device
}
})
}else{
return await {"errcode":1,"errmsg":"您的信息有风险,请修改再发布!"}
}
JS代码:
db.collection("weibo").get().then(res=>{
// console.log(res);
var date = res.data[0].createTime;
// console.log(date);
var time = new Date(date).getTime();
console.log(time);
this.setData({
time:time
})
})
WXS代码:
var timeFormat = function(time){
console.log(time);
var date = getDate(time);
console.log(date);
var date_seconds = date.getTime() / 1000 ;
var now = getDate();
var now_seconds = now.getTime() / 1000;
var timestamp = now_seconds - date_seconds;
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 = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
timeStr = year + "/" + month + "/" + day + " " + hour + ":" + minute;
}
return timeStr;
}
module.exports={
timeFormat:timeFormat,
}
传的值有问题