-1

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];
        }
    }
KFE
  • 637
  • 5
  • 10
  • And the problem is ... – Yury Tarabanko Aug 08 '16 at 18:15
  • So, what have you tried? – J. Titus Aug 08 '16 at 18:15
  • Start by looking into [moment.js](http://momentjs.com/). A great javascript library for managing date and time. – Ozan Aug 08 '16 at 18:17
  • You would do this using javascript Date objects. Date objects contain both a date and a time, but the problems is you do not have a date so it will be difficult to determine which even is *truly* closest. but assuming all dates are "Today" then you can just convert all the start times to Date objects and just do some simple math to determine which is closest: [**see This**](http://stackoverflow.com/questions/1787939/check-time-difference-in-javascript) – fnostro Aug 08 '16 at 18:19
  • all dates are today. i'll check the link thanks – KFE Aug 08 '16 at 18:24

1 Answers1

1

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 + ' ' + 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(); // 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"
    }, ...
]
Jeremy Klukan
  • 228
  • 1
  • 4
  • This code was written with the original sample data that contained a `start_date` field. – Jeremy Klukan Aug 08 '16 at 18:23
  • you have an error in the code `return new Date(event.start_date + ' ' + start_time.replace(/(AM|PM)/, ' $1'));` should be `return new Date(event.start_date + ' ' + event.start_time.replace(/(AM|PM)/, ' $1'));` (missing event on the start_time) – KFE Aug 08 '16 at 18:35