1.微信小程序util.js下有一个日期格式化函数
const formatTime = date => {
var date = new Date(date);
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
module.exports = {
formatTime: formatTime
}
但是它原来是没有下面的这段代码的 它要求传入的data是一个日期参数,为了方便将日期字符串作为参数传递,将date转换一下(这里的参数date就是日期字符串):
var date = new Date(date);
2.获取后台的list列表数据后,遍历对其进行日期格式化:
引入util.js
var util = require('../../utils/util.js');
评论列表日期格式化代码:
if (res.data.result == 0) {
let comms = res.data.lists
console.log(comms)
for(let c in comms){
let date = util.formatTime(comms[c].ctime)
comms[c].ctime = date
}
that.setData({
commentList: comms || [],
bookIsBuy: res.data.is_buy
});
setTimeout(function () {
that.setData({
commentLoading: false
});
}, 500);
}
调用util.js中的方法
let date = util.formatTime(comms[c].ctime)