I have provided a solution based upon the html you have provided in the Jsfiddle.
The input values are mismatching with the displayed values in the jsfiddle, So I corrected them and came up with the below solution.
However, I personally prefer to store the dates while rendering components itself, such that it will help to easily fetch the dates while comparing. :)
Also updated your JsFiddle. :)
var selectedDates = {
business : { time1 : null, time2 : null, node:null},
reliability : { time1 : null, time2 : null, node:null},
engineering : { time1 : null, time2 : null, node:null},
};
var year = (new Date()).getFullYear();
var monthList = { Jan :1 ,Feb :2, Mar :3, Apr:4, May:5, Jun:6, Jul:7, Aug:8,Sep:9,Oct:10,Nov:11,Dec:12}
$( document ).ready(function() {
$(".time").change(function() {
var node= this,name = $(node).attr('name');
if($(node).val() === '~'){
selectedDates[name].time1 = selectedDates[name].time2 = selectedDates[name].node=null;
$('input.time[name="'+name+'"').parents("tr").removeClass("red");
return;
}
var fullDate = $(node).val().split('|')[0], timeList = $(node).val().split('|')[1];
name = $(node).attr('name');
time1 = timeList.split('-')[0].trim();
time2 = timeList.split('-')[1].trim();
day = fullDate.split(' ')[1];
month = day.split('/')[0];
date= day.split('/')[1];
month = monthList[month];
console.log(time1,time2,day,month,date);
selectedDates[name].time1 = new Date(month+'/'+date+'/'+year+' '+time1);
selectedDates[name].time2 = new Date(month+'/'+date+'/'+year+' '+time2);
selectedDates[name].node = $(node);
$('.red').removeClass('red');
for(prop in selectedDates)
{
compareDates(prop);
}
checkInconsistency();
});
function compareDates(name){
for(prop in selectedDates)
{
if(selectedDates[prop].time1 !== null && prop!==name){
if(compareTime(name,prop) || compareTime(prop,name))
{
$(selectedDates[name].node).parents("tr").addClass("red");
$(selectedDates[prop].node).parents("tr").addClass("red");
}
}
}
}
function compareTime(name1,name2){
return (selectedDates[name1].time1 >= selectedDates[name2].time1 && selectedDates[name1].time1 <= selectedDates[name2].time2) ||
(selectedDates[name1].time2 >= selectedDates[name2].time1 && selectedDates[name1].time2 <= selectedDates[name2].time2)
}
function checkInconsistency(){
if($('.red').length === 1)
{
$('.red').removeClass('red');
}
}
});