Mini Program, how to add a method to data rendering and change its value, such as the following figure and code

< H2 > I want to change the red box in the picture to how many days ago < / H2 >

clipboard.png


clipboard.png

clipboard.png

clipboard.png

clipboard.png


write the function in 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>

if there is a better way, please share


the landlord is actually talking about filters, which can be used in templates or even functions directly in both vue.js and angular.js.
but not Mini Program.

there are two general choices:

  1. cycle through the array in the js file, change it to the format you want, and then setData ();
  2. use @ when happiness knocks on the door to say the wxs file, see the official documentation for details.

give two more tips:

  1. new Date (), cannot be used in wxs. Please check the documentation
  2. pay attention to compatibility issues with Apple settings.

this is the ios system, add this value = value.replace (/-/ g,'/') before the let date = new Date (value) of timeChange.


1. First of all, Mini Program needs to use the getDate function to generate the date object, which returns an object of the current time.
2. Mini Program let will report an error

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>

Test this is achievable

Menu