javascript - How to find upcoming event in array based on current time -


i have array of events , i'd iterate through array , find next upcoming event based on start time compared current start time.

basically, 3:30pm , there events starting @ 12:00pm, 2pm, 4pm, 5pm.. i'd event starts @ 4pm.

sample data:

    {   "cache_time": 1470678992,   "events": [     {       "title": "event title",       "start_time": "12:00am",       "end_time": "12:00am"     },     {       "title": "event title",       "start_time": "8:00am",       "end_time": "10:00am"     },     {       "title": "event title",       "start_time": "10:00am",       "end_time": "12:00pm"     },     {       "title": "event title",       "start_time": "1:00pm",       "end_time": "2:30pm"     },     {       "title": "event title",       "start_time": "2:30pm",       "end_time": "3:00pm"     },     {       "title": "event title",       "start_time": "3:00pm",       "end_time": "4:00pm"     }   ] } 

edit:

what i've done far current meeting i'm having trouble getting next meeting when current meeting unknown.

    var _this = this;     var date = new date();     var currenthour = date.gethours();     var currentmins = date.getminutes();     var reqstarttime = new date();      reqstarttime.sethours(currenthour, currentmins, 0);      var reqendtime = new date(reqstarttime.gettime() + 2 * 60000);      (var e in event_data.events) {         var start_time  = event_data.events[e].start_24time;         var end_time    = event_data.events[e].end_24time;          var starttime = new date();         var endtime = new date();           starttime.sethours(start_time.substring(0,2), start_time.substring(3,5), 0);         endtime.sethours(end_time.substring(0,2), end_time.substring(3,5), 0);          if ( (reqendtime > starttime && reqendtime < endtime) || (reqstarttime > starttime && reqstarttime < endtime) ) {             return event_data.events[e];         }     } 

because start_time non-standard format, 1 thing need convert usable value. combine start_date our comparison value.

since can't assume events in order, we'll taking account.

(function(data) {    var currenttime = new date();    var getstarttime = function(event) { // function converts time usable format , returns date object      try {        return new date(event.start_date + ' ' + event.start_time.replace(/(am|pm)/, ' $1'));      } catch(ex) { return null; }    };    var sortedevents = jquery(data.events).sort(function(a, b) {      return getstarttime(a) > getstarttime(b) ? 1 : -1;    }).toarray(); // array of sorted events    (var = 0; < sortedevents.length; i++) {      if (getstarttime(sortedevents[i]) < currenttime) { continue; }      return sortedevents[i]; // starts or after    }    return null; // no matches  })(sample_data);

original sample data question:

"events": [      {        "title": "event title",        "start_time": "12:00am",        "end_time": "12:00am",        "start_24time": "00:00",        "end_24time": "00:00",        "start_date": "july 29, 2016",        "end_date": "september 03, 2016",        "display_date": "july 29 - september 03, 2016"      }, ...  ]


Comments