1.wxs
var public={
myDate: function (value, type = 0){
var time = getDate(value);
var year = time.getFullYear();
var month = time.getMonth() + 1;
var date = time.getDate();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
month = month < 10 ? "0" + month : month;
date = date < 10 ? "0" + date : date;
hour = hour < 10 ? "0" + hour : hour;
minute = minute < 10 ? "0" + minute : minute;
second = second < 10 ? "0" + second : second;
var arr = [
year + "-" + month + "-" + date,
year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second,
year + "年" + month + "月" + date,
year + "年" + month + "月" + date + " " + hour + ":" + minute + ":" + second,
hour + ":" + minute + ":" + second
]
return arr[type];
}
}
module.exports=public
2.wxml引入
<wxs src="../../wxs/public.wxs" module="public" />
3.将云端获取的时间转化为时间戳
getTime()将时间对象转化为时间戳
onLoad: function (options) {
db.collection("Art").get().then(res=>{
res.data.forEach(item=>{
item.post_time=item.post_time.getTime()
})
this.setData({
dataList:res.data
})
})
},
4.将数据放入引入的myDate()方法
<view class="time">{{public.myDate(item.post_time)}}</view>