Reputation: 4082
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
Upvotes: 0
Views: 1803
Reputation: 388316
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>
Upvotes: 3
Reputation: 788
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;
Upvotes: 0
Reputation: 198314
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.
Upvotes: 2