小程序,如何给数据渲染时加一个方法,改变他的值,如下图和代码

我想把图中红色框转为多少天前

clipboard.png

如下方法

<script>
    function replyTime(value) {
        if (!value) {
            return ''
        }
        let date = new Date(value)
        let time = new Date().getTime() - date.getTime() // 现在的时间-传入的时间 = 相差的时间(单位 = 毫秒)
        if (time < 0) {
            return ''
        } else if ((time / 1000 < 30)) {
            return '刚刚'
        } else if (time / 1000 < 60) {
            return parseInt((time / 1000)) + '秒前'
        } else if ((time / 60000) < 60) {
            return parseInt((time / 60000)) + '分钟前'
        } else if ((time / 3600000) < 24) {
            return parseInt(time / 3600000) + '小时前'
        } else if ((time / 86400000) < 31) {
            return parseInt(time / 86400000) + '天前'
        } else if ((time / 2592000000) < 12) {
            return parseInt(time / 2592000000) + '月前'
        } else {
            return parseInt(time / 31536000000) + '年前'
        }
    }
    console.log(replyTime('2018-04-26T09:51:19.808Z'))  //22前天
</script>

clipboard.png

clipboard.png

空白,也不报错

clipboard.png

重新上传

clipboard.png

阅读 2.6k
4 个回答

1.首先 小程序 生成 date 对象需要使用 getDate函数, 返回一个当前时间的对象。
2.小程序let会报错

module.exports = function (value) {
  if (!value) {
    return ''
  }
  var date = getDate(value)
  var time = getDate().getTime() - date.getTime() // 现在的时间-传入的时间 = 相差的时间(单位 = 毫秒)
  if (time < 0) {
    return ''
  } else if ((time / 1000 < 30)) {
    return '刚刚'
  } else if (time / 1000 < 60) {
    return parseInt((time / 1000)) + '秒前'
  } else if ((time / 60000) < 60) {
    return parseInt((time / 60000)) + '分钟前'
  } else if ((time / 3600000) < 24) {
    return parseInt(time / 3600000) + '小时前'
  } else if ((time / 86400000) < 31) {
    return parseInt(time / 86400000) + '天前'
  } else if ((time / 2592000000) < 12) {
    return parseInt(time / 2592000000) + '月前'
  } else {
    return parseInt(time / 31536000000) + '年前'
  }
}
 <wxs src="./ccc.wxs" module="time" /> 
 <view>{{time('2018-04-26T09:51:19.808Z')}}</view>

测试 这样是可以实现的

把函数写在wxml里面

<wxs module="replyTime">
    module.exports = function(value) {
         if (!value) {
            return ''
        }
        var date = new Date(value)
        var time = new Date().getTime() - date.getTime() // 现在的时间-传入的时间 = 相差的时间(单位 = 毫秒)
        if (time < 0) {
            return ''
        } else if ((time / 1000 < 30)) {
            return '刚刚'
        } else if (time / 1000 < 60) {
            return parseInt((time / 1000)) + '秒前'
        } else if ((time / 60000) < 60) {
            return parseInt((time / 60000)) + '分钟前'
        } else if ((time / 3600000) < 24) {
            return parseInt(time / 3600000) + '小时前'
        } else if ((time / 86400000) < 31) {
            return parseInt(time / 86400000) + '天前'
        } else if ((time / 2592000000) < 12) {
            return parseInt(time / 2592000000) + '月前'
        } else {
            return parseInt(time / 31536000000) + '年前'
        }
    }
    console.log(replyTime('2018-04-26T09:51:19.808Z'))  //22前天
    }
</wxs>

题主如果有更好的方法请分享一下

楼主说的其实是过滤器,在 vue.js 和 angular.js 中都可以在模板中使用过滤器,甚至直接使用函数。
但是小程序不行。

一般两种选择:

  1. 在 js 文件中把数组循环一遍,修改成你要的格式后再 setData();
  2. 使用 @当幸福来敲门 说的 wxs 文件,具体请看官方文档。

再给2点小提示:

  1. wxs中不能使用 new Date(),请查一下文档
  2. 注意苹果设置的兼容性问题。
新手上路,请多包涵

你这是ios系统吧,在timeChange的let date = new Date(value)前面加这个 value = value.replace(/-/g, '/');就可以了。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题