<script type="text/javascript">
// Use iso8601 like datestring to make a local date object</font>
function getDateObj(s) {
var bits = s.split(/[- :]/);
var date = new Date(bits[0], bits[1] - 1, bits[2]);
date.setHours(bits[3], bits[4], 0);
return date;
}
var events = [
{id: 1, start: getDateObj('2014-05-1 15:00'), end: getDateObj('2014-05-2 03:00')},
{id: 2, start: getDateObj('2014-05-1 07:00'), end: getDateObj('2014-05-2 11:00')},
{id: 3, start: getDateObj('2014-05-5 18:20'), end: getDateObj('2014-05-5 23:00')},
{id: 4, start: getDateObj('2014-05-4 23:00'), end: getDateObj('2014-05-5 20:00')},
{id: 5, start: getDateObj('2014-05-3 20:00'), end: getDateObj('2014-05-3 22:00')}
];
function getOverlappingEvents(eventArray) {
var result = [];
var a, b;
// Sort the event array on start time
eventArray.sort(function(a, b) {
return a.start - b.start;
});
// Get overlapping events
for (var i=0, iLen=eventArray.length - 1; i<iLen; i++) {
a = eventArray[i];
b = eventArray[i + 1];
if ((a.start <= b.start && a.end > b.start) ||
(a.start < b.end && a.end >= b.end) ) {
result.push([a.id, b.id]);
}
}
return result;
}
// Run it
alert(getOverlappingEvents(events).join('\n')); // 1,3 2,3 5,6
//</script>
Big thank : http://codeformatter.blogspot.com/
Pages
▼
No comments:
Post a Comment