如何让js 判断,所选时间是否在某一时间段并返回符合条件的内容


filterByTime(tableData, time) {
        return this.tableData.filter(item =>item.time_TT == time)
      },
      isDuringDate(curDate, beginDateStr, endDateStr) {
        var curDate = new Date(curDate),
          beginDate = new Date(beginDateStr),
          endDate = new Date(endDateStr);
        if (curDate >= beginDate && curDate <= endDate) {
          this.filterByTime(this.tableData,curDate)
        } else {
          console.log('Str')
          this.tableData = []
        }
      },
 TTwebsocket.onmessage = function(res) {//websocke
          let val = JSON.parse(res.data)
          that.tableData = JSON.parse(val.listLogs)
 for (let i = 0; i < that.tableData.length; i++) {     
            if (that.message.starTime !== '' || that.message.endTime !== '') {
              that.isDuringDate(that.tableData[i].time_TT, that.message.starTime, that.message.endTime)
            }
          }

这判断不对吗

img

后台给的时间值和用户输入的时间值格式不一样,是不能进行判断吗?


console.log(that.message.starTime) //2022-03-02 00:00:00--用户的
console.log(that.tableData[i].time_TT)//2022-01-02--后台的

你要转成一样的格式比较

转成 毫秒数 就好 比较了 。 用moment.js 转 原生也行