title: How to filter array from one date to another in Reactjs
body: <p>I have an array of objects &quot;mainData&quot; like so:</p>
  <pre><code>0: {date: &quot;2020-07-25T16:44:43.000Z&quot;
  description: &quot;Qwerty&quot;
  id: 89329972},
  1: {date: &quot;2020-07-25T16:46:28.000Z&quot;
  description: &quot;Place bins please&quot;
  id: 65586316},
  2: {date: &quot;2020-07-25T16:49:12.000Z&quot;
  description: &quot;Solve sewerege problem&quot;
  id: 84687816},
  3: {date: &quot;2020-07-27T16:34:47.000Z&quot;
  description: &quot;Test compl&quot;
  id: 56437370},
  4: {date: &quot;2020-07-28T08:40:34.000Z&quot;
  description: &quot;Sewerage problem in my area&quot;
  id: 92402221},
  5: {date: &quot;2020-09-09T11:42:18.000Z&quot;
  description: &quot;problem&quot;
  id: 25613902},
  
  </code></pre>
  <p>Now I am allowing the user to select from and to dates by using the mui datepicker. This is how I am receiving the values:</p>
  <pre><code>fromDate: Sat Jul 25 2020 11:43:00
  toDate: Sat Aug 08 2020 11:43:00
  </code></pre>
  <p>Now I want to filter the array from this date to that date, including the from and to dates. I tried to do it this way but it just returns an empty array. I've put the code inside useEffect which is run every time toDate changes, Also I've used Moment to make the formats of both dates same:</p>
  <pre><code> useEffect( () =&gt; {
          if (fromDate !== null &amp;&amp; toDate !== null) {
              setReportData(
                  mainData.filter(
                      (obj) =&gt;{
                          
  
                          return Moment(obj.date).format(&quot;DD MMM yyyy&quot;) &gt;= Moment(fromDate).format(&quot;DD MMM yyyy&quot;) &amp;&amp; Moment(obj.date).format(&quot;DD MMM yyyy&quot;) &lt;= Moment(toDate).format(&quot;DD MMM yyyy&quot;)
                      }
  
  
                  )
              )
  
             
  
          }
  
      },[toDate])
  </code></pre>
  <h2>Edit</h2>
  <p>When I select a single date :</p>
  <pre><code> useEffect( () =&gt; {
          if (oneDate !== null) {
  
              setReportData(
                  mainData.filter(
                      (obj) =&gt;{
                     
  
                          return new Date(obj.date.substring(0, 19)).getTime() === oneDate.getTime()
                      }
  
  
                  )
              )
  
          }
  
      },[oneDate])
  </code></pre>