0

I need to check the time interval between two time period. ie needs to block the appointment of a person in client side.

I have got 2 arrays

var fromTimings=["8:00","12:00","","16:00"];
var toTimings=["9:10","01:00","","19:00"];

These are the blocks which are already booked. ie first appointment is booked from 8:00 to 9:10, second is 12:00 to 1:00 and so on.

All the appointments are stored in two arrays in the above format.

Now I need to check for a user defined slot.

ie if the user entered like fromTime = 9:15 and 10:00 then must return / show available

logic as follows

fromTime    |   toTime     |   result
------------+--------------+-----------
7:00        |   7:59       |   true
8:10        |   10:00      |   false ( already meeting between 8:00 to 9:10)
19:01       |   23:59      |   true

Can anyone please help ?

Thanks in advance

3
  • 2
    why are you using separate arrays for from/to instead of "block" objects with a from and to time that you can lock or unlock as necessary? Commented Sep 30, 2014 at 3:36
  • Hi thanks for your reply. can you please explain a little bit ? we can avoid two arrays of course
    – ramesh
    Commented Sep 30, 2014 at 3:40
  • use something like a var Timeslot = new function(f,t) { this.from=f; this.to=t; this.reserved=false; }; Timeslot.prototype = { toggle: function() { this.reserved = !this.reserved; }}; and now you can simply build new Timeslot(...,...) object and toggle each slot based on whether it's taken. Add a function to see if a time falls in a slot to the prototype, and off you go. Commented Sep 30, 2014 at 3:52

3 Answers 3

3

In the below code, for the easiness of comparison the hh:mm formatted time is converted to mins

//just for logging the messages
var log = (function() {
  var $log = $('#log');
  return function(msg) {
    $('<p/>', {
      text: msg
    }).appendTo($log)
  }
})();


var fromTimings = ["8:00", "12:00", "", "16:00"];
var toTimings = ["9:10", "01:00", "", "19:00"];

function test(from, to) {
  var f = toMins(from),
    t = toMins(to),
    ft, tt;
  for (var i = 0; i < fromTimings.length; i++) {
    ft = toMins(fromTimings[i]), tt = toMins(toTimings[i]);
    if ((f >= ft && f <= tt) || (t >= ft && t <= tt)) {
      return false;
    }
  }
  return true;
}

function toMins(time) {
  var parts = time.split(':');
  return (+parts[0] * 60 + +parts[1]) || 0;
}

function testTime(from, to) {
  log(from + '-' + to + ': ' + test(from, to))
}

testTime('07:00', '07:59');
testTime('8:10', '10:00');
testTime('19:01', '23:59');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="log"></div>

0
2

Loop through your reserved times and check each period for overlap with the query period.

Two periods overlap if the start of first is before end of second, and end of first after start of second.

0

The time period that has appointment is (8:00~9:10),(12:00 ~ 13:00)and (16:00~19:00). So if the time interval given is (A~B):

if(B<8)
 return true;
if(A>8&&B<9:10)
 return true;
if(A>12&&B<13)
 return ture;
if(A>13&&B<16)
 return true;
if(A>19)
 return true
return false;

Not the answer you're looking for? Browse other questions tagged or ask your own question.