-1

I am facing an issue in React: I have some JSON data and I want to get the starttime or endtime from it. How can I do that?

json data (multiple dates)

data {starttime: "14:30:00", endtime: "15:30:00"}
data {starttime: "15:30:00", endtime: "17:30:00"}
data {starttime: "16:30:00", endtime: "18:30:00"}
data {starttime: "18:30:00", endtime: "19:30:00"}

react component

.map(({ full_name, breaks }) => (
  <span className="more-space" key={full_name}>
    { full_name }<br />
    {
      breaks.filter(data => {
        console.log("start", data.starttime);  //start 14:30:00
        console.log("end", data.endtime);  //end 15:30:00
        const date = new Date(data.starttime);

        const date = new Date(data.starttime);    //date Invalid Date
        const enddate = new Date(data.endtime);   //date Invalid Date
        return date >= prevDate && date < nextDate;
      }
    }
)
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
MHasan
  • 69
  • 1
  • 8

1 Answers1

0

Issue is with const date = new Date(data.starttime);. Date constructor will not accept the just time format.

Try something like below, create date and then update hours, mins and sec

const starttime = "14:30:00";

const [hours, mins, sec] = starttime.split(':').map(x => Number(x));
const sdate = new Date();
sdate.setHours(hours);
sdate.setMinutes(mins);
sdate.setSeconds(sec);

console.log(sdate.toString());
Siva K V
  • 10,561
  • 2
  • 16
  • 29