想要实现,将数据库的时间转换为,今天,明天,后天,具体日期等。wxs写了段代码,打印结果是获取的数据库时间为Invalid Date 。数据库保存的时间两种方式和打印结果如图:在发布信息时,无论选择哪天,显示的都是今天。数据库保存的时间类型会影响转换吗?
第二种
wxs代码:
var timeFormat = function (dateString) {
var date = getDate(dateString);// 传递日期字符串,得到日期
console.log('数据库的日期',date);
var today = getDate() // 当前的时间
console.log('当前的日期',today)
var timeStr = '';
var daysDifference = Math.abs(date.getTime() - today.getTime());
// console.log(daysDifference)
var daysBetween = Math.floor(daysDifference/(1000*60*60*24));
console.log('天数差',daysBetween)
var year = date.getFullYear();
var month = ('0' + (date.getMonth() + 1)).slice(-2);
var day = ('0' + date.getDate()).slice(-2);
var isToday = daysBetween === 0 ? '今天':'';
var isTomorrow = daysBetween === 1 ? '明天':'';
var isDayAfterTomorrow = daysBetween === 2 ? '后天':'';
if(isToday){
timeStr = isToday;
}else if(isTomorrow){
timeStr = isTomorrow;
}else if(daysBetween = isDayAfterTomorrow){
timeStr = isDayAfterTomorrow;
}
else{
timeStr = year + "/" + month + "/" + day
// timeStr = dateString
}
return timeStr;
}
传进来的datestring 是啥 打印下,看看 符合文档里的格式吗
components代码:
<wxs src="../../utils/util.wxs" module="util" />
<view class="start-time">{{util.timeFormat(vehicle.day)}} {{vehicle.times}} 出发</view>
omponents.js代码:
properties: {
vehicle: {
type: Object,
value: {}
},
},
index.wxml代码
<block wx:if="{{isQuerying == true}}">
<vehicle wx:for="{{vehicles}}" wx:key="vehicle" wx:for-item="vehicle" vehicle="{{vehicle}}"></vehicle>
</block>
index.js代码:
newVehicles.forEach((vehicle, index) => {
vehicle.create_time = vehicle.create_time.toString();
vehicle.day = vehicle.day.toString() // 这段代码有没有都一样
});