新建一個.wxs
文件code
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中引入文件xml
<!-- 引入.wxs文件 src爲相對路徑,module指定當前模塊的名稱 --> <wxs module="filter" src="./func.wxs"></wxs>
<view>{{filter.getDateTime(時間戳)}}</view>