我有一个循环,该循环会在不可用的情况下引起时间愤怒
喜欢:
{'start'=>'06:00 pm','end'=>'08:00 am'} // i will be not available after 6PM
{'start'=>'09:00 am','end'=>'11:00 am'} // i will be not available b/w this range
如果我给定的时间不在给定的范围内,我正在做的事情就是以30分钟为间隔运行一整天的循环,这将表明我当时有空。
我的代码:
function isInBlockRange(blockdata,time)
{
var tReturn = false;
if(blockdata.length>0)
{
var currentTime = moment(time,'hh:mm a');
for(var i = 0; i < blockdata.length; i++){
var BlockTimseStart = moment(blockdata[i].start,'HH:mm a');
var BlockTimseEnd = moment(blockdata[i].end,'HH:mm a');
if ( (BlockTimseStart.hour() >=12 && BlockTimseEnd.hour() <=12 ) || BlockTimseEnd.isBefore(BlockTimseStart) )
{
BlockTimseEnd.add(1, "days"); // handle spanning days endTime
if (currentTime.hour() <=12 )
{
currentTime.add(1, "days"); // handle spanning days currentTime
}
}
if(currentTime.isBetween(BlockTimseStart,BlockTimseEnd, null, '[)') == false)
{
tReturn = false;
}
else
{
tReturn = true;
}
}
return tReturn;
}
else
{
return true;
}
}
////循环调用方法
for (var i = 0; i < 48; i++) {
time = moment(time, 'hh:mm A').add((30), 'm').format('hh:mm A');
if(isInBlockRange(blockTimeArray,time) == false){
console.log("available");
}
}