console.log(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,
}
wxml代码
<!--index.wxml-->
<wxs src="../../utils/util.wxs" module="util"/>
<view class="container">
<view class="weibo-group" wx:for="{{weibos}}" wx:key="weibos" wx:for-item="weibo">
<view class="user-group">
<image class="avatar" src="{{weibo.author.avatarUrl}}"></image>
<view class="right-info">
<view class="username">{{weibo.author.nickName}}</view>
<view class="detail-info">
<view class="time">{{util.timeFormat(weibo.cretae_time)}}</view>
<view class="from">来自{{weibo.device}}</view>
</view>
</view>
</view>
云函数代码:
return await db.collection("weibo").add({
data:{
content:content,
location:location,
author:author,
images:images,
video:video,
cretae_time:db.serverDate(),
device:device
}
})
实在不行别用wxs了,在list中直接转换成你想要的值吧
e.time = timeFormat(new Data(time));
}
然后直接用time,这种不知道是不是你想要的结果
https://blog.csdn.net/m0_55681803/article/details/123899132
看起来像cretae_time生成的就有问题呢,format函数应该是没有问题的
这里打印一下 date ,看看是个啥
time先处理一下
if (!time) return "";
var reg = getRegExp("-", "g");
time = time.replace(reg, '/');
var date = getDate(time);
create_time是什么样的,是不是YYYY-MM-DD这种的?ios不能转化带-的,只能转化带/的
这里是获取传入的时间的意思吗?这么写试试
var date = new Date(time)
还有这处,getDate()函数是哪里有定义吗