iT邦幫忙

0

Vue Date Filter 無法撈出當天資料

HI~ 大家好,最近開始接觸Vue,想請問一個Vue Date Filter無法撈出當天資料的問題,目前有兩個data table要透過Date Filter來過濾資料,目前資料建立日期都是2021/06/05,當filter條件設定成2021/06/05到2021/06/05會無法撈出2021/06/05的資料 如下圖:
https://ithelp.ithome.com.tw/upload/images/20210606/20107444SbJSVdkwHt.png
但如果條件設定成2021/06/05到2021/06/06 就能撈出2021/06/05的資料 如下圖:
https://ithelp.ithome.com.tw/upload/images/20210606/20107444plSl63GkRN.png
請問是哪邊判斷有錯誤嗎?
以下為判斷的Function還請大大們指正:

computed: {
    
    filterWorkhours() {
      let filterType = this.selectedType;
      let startDate = this.localizeDate(this.startDate);
      let endDate = this.localizeDate(this.endDate);

      const itemsByType = filterType
        ? this.workhours.filter((workhours) => workhours.type === filterType)
        : this.workhours;
      return itemsByType.filter((workhours) => {
        const itemDate = new Date(workhours.date);
        if (startDate && endDate) {
          return startDate <= itemDate && itemDate <= endDate;
        }
        if (startDate && !endDate) {
          return startDate <= itemDate;
        }
        if (!startDate && endDate) {
          return itemDate <= endDate;
        }
        return true;
      });
    },
  },
DanSnow iT邦好手 1 級 ‧ 2021-06-07 00:27:05 檢舉
這是猜測,因為沒看到你實際的資料沒辦法判斷, js 的 Date 是含有時間的,但你那個選日期的,轉成 Date 後我猜它會把時間設定成 00:00:00 ,但你的資料的 6/5 有可能實際上它們也是有時間資料的,比如 11:00 ,所以你的判斷式就變成找 6/5 00:00:00 ~ 6/5 00:00:00 這段時間的資料,那 11:00 的資料就不會被選進去

假如是這樣,解決方法有兩種
1. 把 endDate 的時間都設定成 23:59:59
2. 把 itemDate 的時間都設定成 00:00:00
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答