0

I have Schedule array with start/end date time and its layouts name.

let schedule=[
  {
    "start": "2017-10-01 08:00:00",
    "end": "2017-10-01 09:00:00",
    "layout": "layout1.json"
  },
  {
    "start": "2017-10-01 10:00:00",
    "end": "2017-10-01 12:00:00",
    "layout": "layout2.json"
  },
]

let current_time = "2017-10-01 10:30:00";

If current dateTime is **current_time** in need to get layout name layout2.json. need to search between dateTime from above array using JavaScript.

I have try below code but its not working ..

 for (i = 0; i < schedule.length; i++){
   if (schedule[i].start >= current_time && schedule[i].end <= current_time){  
     return schedule[i].layout;
    }
 }

Help me, Thanks

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
bijus
  • 161
  • 8

1 Answers1

1

You need to convert the date to a js Date before comparing.

for (i = 0; i < schedule.length; i++){
    if (new Date(schedule[i].start)<= new Date(current_time) && new Date(schedule[i].end) >= new Date(current_time)){  
      return schedule[i].layout;
     }
 }
Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98