JS&时间范围 - 无法写出正确的IF语句

时间:2016-03-11 11:48:32

标签: javascript if-statement time

我正在制作一个显示天气的小工具现在是否有工作时间。很简单,只需将当前时间与start&时间结束!但如果公司'从晚上到早上的工作时间:

  var atWork = 'false';
    var start = 22; //working day  starts
    var end = 7; //working day  ends
    var nowt = 1; //

    if (start>=end)   {//nightshift
       end+=12; start -=12; 
        if (nowt > 0 && nowt <12) nowt += 12; else nowt-=12; 
       if (start < nowt && nowt< (end-12)){ atWork = 'true'; }
    } 
    else { //normal
        if (nowt >= start && nowt <end){atWork = 'true'; }
    } 
    $('#debug').html(atWork+' start: '+start+' end: '+end+'  curtime: '+nowt);

感谢gurvinder372我已经找到了这个解决方案......但仍然没有正常工作(

3 个答案:

答案 0 :(得分:2)

function check(start, end, current) {
    return ((current >= start || current <= end) && end <= start) || 
            (current <= end && current >= start);
}

答案 1 :(得分:1)

有趣的问题!

这显然是夜班的情景,你需要把它带到白班:)

var atWork = false;
var start = 8; //working day starts from 8 o'clock  
var end = 19; //working day ends at 19 o'clock  
var nowt = servertime(gmt);

/*****this part has been added****/
if (nightShift)
{
   end += 12; start -= 12; nowt -= 12;
   if (nowt < 0)
   {
     nowt += 12;
   }
}
/*********************************/

if (nowt >= start && nowt <end)
{
   atWork = true; 
}

请注意,此逻辑假定班次仅为8-12小时。

答案 2 :(得分:0)

  if ((start >= end) && (t >= start || t <= end)){
   atWork = 'true'; }