WeChat applet-cutting string in wxml

problem

Need to cut item.letter_date ("2020-10-02 09:02:01") into "2020-10"

<view class="letter-time">{
   
   {item.letter_date.substr(0,7)}}</view>

solve

Use of WeChat Mini Program wxs: Documentation

  • Create a wxs file and encapsulate the function you want to call

    module.exports.substringThe function is to expose this substring function and share the private variables and functions of this module with the outside world.

    var substring = function (val, start, end) {
          
          
        if (val.length == 0 || val == undefined) {
          
          
            return;
        }
        return val.substring(start, end);
    }
    module.exports.substring = substring;
    
  • Quote in wxml, src is a relative path, module is arbitrary

    <wxs  src="myMail.wxs" module="tools" />
    
  • transfer

    <view class="letter-time">{
         
         {strUtils.substring(item.letter_date,0,7)}}</view>
    

Guess you like

Origin blog.csdn.net/weixin_42100456/article/details/108900711