1,在utils文件夹里创建filter.wxs
2,在filter.wxs中添加如下代码:
var formatNumber = function (n) {
n = n.toString()
return n[1] ? n : '0' + n
}
var formatDate = function (datetime) {
var date = getDate(datetime);
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
return [year, month,day].map(formatNumber).join('-')+ ' ' +[hour,min,sec].map(formatNumber).join(':');
}
module.exports = {
formatDate: formatDate
}
3,在wxml中使用:
<wxs src="../../utils/filter.wxs" module="filters"/>
<text>{{filters.formatDate(item.date)}}</text>