wxs中getDate(datestring)在IOS下有BUG
发布于 5 年前 作者 yifang 7583 次浏览 来自 问答
<wxs module='format'>
    var date = function(t) {
        var regexp = getRegExp('-', 'g');
        t = t.replace(regexp, '/');
        var now = getDate().getTime();
        var time = getDate(t).getTime();
        if (time > now) {
            return '刚刚';
        } else {
            var e = Math.round((now - time) / 1000);
            if (e < 60) return '刚刚';
            else if (e < 1800) return Math.round(e / 60) + '分钟前';
            else if (e < 3600) return '半小时前';
            else if (e < 86400) return Math.round(e / 3600) + '小时前';
            else if (e < 86400 * 7) return Math.round(e / 86400) + '天前';
            else if (e < 86400 * 30) return Math.round(e / (86400 * 7)) + '周前';
            else if (e < 86400 * 365) return Math.round(e / (86400 * 30)) + '个月前';
            else return Math.round(e / (86400 * 365)) + '年前';
        }
        return '';
    };
    module.exports = {
        date: date
    };
</wxs>
<text class='time'>{{format.date(addtime)}}</text>

目前我已经通过regexp正则替换掉了“-”为“/”。

默认情况下数据库拿到的日期格式为“2018-08-30 12:00:00”,如果不替换“-”的话,在IOS下是不能通过getDate(datestring)获取到日期对象的。Android下两种格式均表现正常,这是否是个BUG呢?还求官方解答。

2 回复

这是js引擎对日期的解析不一致造成的。

这个是解析引擎不同 造成的 苹果浏览器也会有这个情况,只要返回的时间精确到小时 就转换出NAN 得替换 -  为 /

回到顶部