var filter = {
getDateTime: function (value) {
//不能使用 new Date()
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;
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}
}
// 导出对外暴露的属性
module.exports = {
getDateTime: filter.getDateTime
}
在.wxml中引入文件
在顶部引入:
<wxs module="filter" src="./func.wxs"></wxs>
使用:
<view>{{filter.getDateTime(时间戳)}}</view>
遇到了这么一个问题,就是后台给我的时间是2019-07-23T13:46:51.000+0000
然而ios不支持!!!
后来发现,可以转成Z结尾的value = value.replace(".000+0000","Z");
ios和android就都支持了。
<wxs module="a">
var change = function(value) {
value = value.replace(".000+0000","Z");
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;
return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}
module.exports.change = change
</wxs>
<view>{{a.change(time)}}</view>
data: {
// time:"2017-12-28T10:43:49Z"
time:"2019-07-23T13:46:51.000+0000"
},