wxs时间格式化页面时间是正常的,格式化服务器的时间就显示NaN,百度上查很多,有的说,db.serverDate()创建的时间,在wxml中无法解析。怎么解决?
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,
}
createTime是Date类型的,wxs处理不了,两种方案:1.js中处理,不要用wxs了。2.js中setData的时候转成YYYY/MM/DD HH:mm:ss字符串格式的,再用wxs处理就没问题了
这个是云控制台数据库的截图