KFE KFE - 3 months ago 9
Javascript Question

How to find upcoming event in array based on current time

I have an array of events and i'd like to iterate through that array and find the next upcoming event based on its start time compared to the current start time.

Basically, say its currently 3:30pm and there are events starting at 12:00pm, 2pm, 4pm, 5pm.. I'd like to get the event that starts at 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 so far to get the CURRENT meeting but i'm having trouble getting the next meeting when the current meeting is 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);

for (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];
}
}

Answer

Because your start_time is a non-standard format, one thing we will need to do is convert it into a usable value. We will combine it with the start_date to get our comparison value.

Since we can't assume the events are in order, we'll also be taking that into account.

(function(data) {
  var currentTime = new Date();
  var getStartTime = function(event) { // This function converts the time into a usable format and returns it as a Date object
    try {
      return new Date(event.start_date + ' ' + 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(); // Get an array of sorted events
  for (var i = 0; i < sortedEvents.length; i++) {
    if (getStartTime(sortedEvents[i]) < currentTime) { continue; }
    return sortedEvents[i]; // Starts now or after
  }
  return null; // No matches
})(sample_data);

Original sample data from 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