简体   繁体   English

无法按日期过滤日期

[英]Unable to filter dates in angular

I have written the following code 我写了下面的代码

 this.downloadData.filter(
      m => new Date(m.LogTime) >= new Date(localStorage.getItem('fromDate')) && new Date(m.LogTime) <= new Date(localStorage.getItem('toDate'))
      );

But it never filter data , i need help . 但是它从不过滤数据,我需要帮助。 Values are shown below: 值如下所示:

localStorage.getItem('fromDate'): Fri Sep 07 2018 09:10:27 GMT+0530 (India Standard Time) localStorage.getItem('fromDate'):2018年9月7日星期五格林尼治标准时间+0530(印度标准时间)

localStorage.getItem('toDate'): Fri Sep 07 2018 09:30:27 GMT+0530 (India Standard Time) localStorage.getItem('toDate'):2018年9月7日星期五09:30:27 GMT + 0530(印度标准时间)

Array item 1 is below 数组项目1在下面

CPUStatus
:
"23"

LogTime
:
"9/7/2018 9:20:15 AM"
MemoryStatus
:
"24"

Array item 2 is below 数组项目2在下面

CPUStatus
:
"23"
LogTime
:
"9/7/2018 9:02:15 AM"
MemoryStatus
:
""

Once after filter when i do console.log(this.downloadData); 一旦过滤后我做console.log(this.downloadData); I am getting non filtered results. 我得到非过滤结果。

Ideally it should not show the second array item, since time i selected Fri Sep 07 2018 09:10:27 GMT+0530 (India Standard Time and Fri Sep 07 2018 09:30:27 GMT+0530 (India Standard Time) is not in between second array logtime value which is 9/7/2018 9:02:15 AM. 理想情况下,它不应该显示第二个数组项,因为我选择的时间是星期五2018年9月7日星期五09:10:27 GMT + 0530(印度标准时间,而星期五我选择2018年9月7日星期五9:30:27 GMT + 0530(印度标准时间)不是在第二个阵列logtime值之间,即9/7/2018 9:02:15 AM。

Only first one should show, what i am doing wrong?Why both array items are listing without any filter? 只有第一个应该显示我在做什么错为什么两个数组项都列出而没有任何过滤器?

I fixed it myself 我自己修好了

this.downloadData = this.downloadData.filter(
      m => new Date(m.LogTime) >= new Date(localStorage.getItem('fromDate')) && new Date(m.LogTime) <= new Date(localStorage.getItem('toDate'))
      );

Check below logic and let me know 检查以下逻辑,让我知道

  var fDate = Date.parse(localStorage.fromDate); // parse to date object
  var tDate = Date.parse(localStorage.toDate);

  this.downloadData = this.downloadData.filter(
          m => fDate-Date.parse(m.LogTime)<=0 && Date.parse(m.LogTime)-tDate<=0
          );

Let me know if its helpful 让我知道它是否有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM