鍍金池/ 問答/Java  PHP  Python  HTML/ 小程序,如何給數(shù)據(jù)渲染時(shí)加一個(gè)方法,改變他的值,如下圖和代碼

小程序,如何給數(shù)據(jù)渲染時(shí)加一個(gè)方法,改變他的值,如下圖和代碼

我想把圖中紅色框轉(zhuǎn)為多少天前

clipboard.png

如下方法

<script>
    function replyTime(value) {
        if (!value) {
            return ''
        }
        let date = new Date(value)
        let time = new Date().getTime() - date.getTime() // 現(xiàn)在的時(shí)間-傳入的時(shí)間 = 相差的時(shí)間(單位 = 毫秒)
        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) + '小時(shí)前'
        } 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

空白,也不報(bào)錯(cuò)

clipboard.png

重新上傳

clipboard.png

回答
編輯回答
小曖昧

1.首先 小程序 生成 date 對(duì)象需要使用 getDate函數(shù), 返回一個(gè)當(dāng)前時(shí)間的對(duì)象。
2.小程序let會(huì)報(bào)錯(cuò)

module.exports = function (value) {
  if (!value) {
    return ''
  }
  var date = getDate(value)
  var time = getDate().getTime() - date.getTime() // 現(xiàn)在的時(shí)間-傳入的時(shí)間 = 相差的時(shí)間(單位 = 毫秒)
  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) + '小時(shí)前'
  } 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>

測(cè)試 這樣是可以實(shí)現(xiàn)的

2018年6月18日 07:52
編輯回答
單眼皮

你這是ios系統(tǒng)吧,在timeChange的let date = new Date(value)前面加這個(gè) value = value.replace(/-/g, '/');就可以了。

2017年5月29日 15:39
編輯回答
六扇門

把函數(shù)寫在wxml里面

<wxs module="replyTime">
    module.exports = function(value) {
         if (!value) {
            return ''
        }
        var date = new Date(value)
        var time = new Date().getTime() - date.getTime() // 現(xiàn)在的時(shí)間-傳入的時(shí)間 = 相差的時(shí)間(單位 = 毫秒)
        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) + '小時(shí)前'
        } 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>

題主如果有更好的方法請(qǐng)分享一下

2017年1月4日 05:24
編輯回答
晚風(fēng)眠

樓主說的其實(shí)是過濾器,在 vue.js 和 angular.js 中都可以在模板中使用過濾器,甚至直接使用函數(shù)。
但是小程序不行。

一般兩種選擇:

  1. 在 js 文件中把數(shù)組循環(huán)一遍,修改成你要的格式后再 setData();
  2. 使用 @當(dāng)幸福來敲門 說的 wxs 文件,具體請(qǐng)看官方文檔。

再給2點(diǎn)小提示:

  1. wxs中不能使用 new Date(),請(qǐng)查一下文檔
  2. 注意蘋果設(shè)置的兼容性問題。
2018年1月3日 13:07