0

I am facing an issue in javascript dates, i want to match slot dates comparing start_date or end_date.

my component

  {this.state.data && this.state.data.length
            ? this.state.data
                .filter(data => {
                  const date = new Date(data.start_date);
                  const enddate = new Date(data.end_date);
                  console.log(date); //start
                  console.log(enddate); //end
                  return date > prevDate && date < nextDate;
                })
                .map(({ cust_full_name, start_date }, index) => (
                  <div class="row" key={index}>
                    slot: {index + 1}
                    <p>{start_date}</p>
                    <p>{cust_full_name}</p>
                  </div>
                ))
            : null}


working demo:

https://codesandbox.io/s/lucid-dream-gl3l7?file=/src/App.js

what should i do? can anyone help me?

adnan khan
  • 101
  • 1
  • 3
  • 7

1 Answers1

0

Edit: Your slot values in your state are in arrays but when you pass them to date you aren't accessing the string value.

slot1 : ["date"] Incorrect: new Date[slot1] Correct: new Date[slot[0]]

In your sandbox it seems you never populated newprevious and newseconddate here:

const prevDate = new Date(this.state.newprevious);
const nextDate = new Date(this.state.newseconddate);

Once i populated those in the above state section, it seems to work. I do also suggest maybe turning Date into something like epoch; so the numbers are easier to work with (Get epoch for a specific date using Javascript)

xPaillant
  • 126
  • 6
  • sorry i updated code you check it `this.state.slot1 and this.state.slot` [@](https://stackoverflow.com/questions/3367415/get-epoch-for-a-specific-date-using-javascript) – adnan khan May 11 '20 at 23:06
  • plz help me , i try to solve this issue i did lot of time on it [@](https://stackoverflow.com/users/6580912/xpaillant) – adnan khan May 11 '20 at 23:09
  • @adnankhan thanks for updating! Your slot1,2,3,4 are in arrays. so you would have to to do this.state.slot1[0]. Also the current way slot 1 still wont match because 3:00 > 3:00 won't be true, so maybe make it 3:00 >= 3:00 in (`date > prevDate`) – xPaillant May 11 '20 at 23:10
  • can you make `codesandbox` example plz then can help me [@](https://stackoverflow.com/users/6580912/xpaillant) – adnan khan May 11 '20 at 23:32
  • https://codesandbox.io/s/festive-saha-gupff?file=/src/App.js check updates lines 40-46 and 68-71 – xPaillant May 11 '20 at 23:38
  • your `App.js` code is not show [@](https://stackoverflow.com/users/6580912/xpaillant) – adnan khan May 12 '20 at 00:26
  • @adnankhan apologies, https://codesandbox.io/s/youthful-cartwright-cbtrk?file=/src/App.js. hope this helps! – xPaillant May 14 '20 at 06:35