I use wxs on wepy. The following section of the original js code that formats the timestamp that I have tested in the html file is normal.
now it"s time to change it to the syntax supported by wxs. But the regular understanding of js is still relatively poor, tested many times but still can not correctly change the RegExp.
wxml to call this method is:
<view class="item-time">{{timeFilter.getTime(item.create_time, "yyyy-MM-dd hh:mm:ss")}}</view>
The js section introduces this wxs code as follows:
import timeFilter from "../wxs/filter.wxs"
wxs = {
timeFilter
}
here is the complete code of the filter.wxs
the two parameters passed in by the getTime method, time is the timestamp (accurate to seconds), fmt is yyyy-mm-dd).
module.exports = {
getTime: function (time, fmt) {
if (!time) return ""
var date = getDate(time * 1000)
var o = {
"M+": date.getMonth() + 1, //
"d+": date.getDate(), //
"h+": date.getHours(), //
"m+": date.getMinutes(), //
"s+": date.getSeconds(), //
"q+": Math.floor((date.getMonth() + 3) / 3), //
"S": date.getMilliseconds() //
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length))
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
}
}
return fmt
}
}
if you use the above code directly, the error message is as follows:
ask all the gods to help modify it, thank you very much!