微信小程序裏兩種比較時間的方法

說明:javascript

end_time是數組時的其中一個對象裏的字段

1.使用過濾器java

wxml數組

引用文件
<wxs src="./filter.wxs" module="filterNum" />

使用方法
<view class='label'wx:if="{{filterNum.time(item.end_time)}}">活動中</view> 

wxs過濾器this

var time = function(endtime){
  var bijiao = getDate(endtime) - getDate(); //判斷結束時間減去當前時間 ,若是當前時間大於0則爲True
  // 6.27   - 5.28
  return bijiao > 0 ? true : false;
}

module.exports = {
  time: time
}

  

第二種 直接在請求接口時循環判斷 url

 loadData: function() {
    utoken = wx.getStorageSync("utoken");
    var that = this;
    server.sendRequest({
      url: '',
      data: {
        utoken: utoken
      },
      method: "GET",
      success: function(res) {
        var n = new Date(); //定義並獲取當前時間 
        //格式化日期
        let list = res.data;
        var end = []; //定義結束時間 
        for (let i = 0; i < res.data.length; i++) {

        //判斷結束時間減去當前時間 ,若是當前時間大於0則爲True
          let value = new Date(res.data[i].end_time) - new Date();
          let isActive = value > 0 ? true : false;
          list[i].isActive = isActive;
        }
        that.setData({
          bargin: list
        })
      }
    })
  },
相關文章
相關標籤/搜索