match values start or end dates in javascript

一曲冷凌霜 提交于 2020-05-17 06:36:20

问题


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?


回答1:


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)



来源:https://stackoverflow.com/questions/61740795/match-values-start-or-end-dates-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!