Andrew Bro
Andrew Bro

Reputation: 491

JS & time range - can't write correct IF statement

Im making kinda widget showing weather now is working time or not. Pretty easy just match current time with start & end time! But the whole logic fails if Company' working hours from evening to morning:

  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);

thanks to gurvinder372 i've came up to this solution... But still not working properly(

Upvotes: 0

Views: 43

Answers (3)

Andrew Bro
Andrew Bro

Reputation: 491

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

Upvotes: 0

Vitaly Kulikov
Vitaly Kulikov

Reputation: 723

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

Upvotes: 2

gurvinder372
gurvinder372

Reputation: 68393

Interesting problem!

This clearly is a scenario of a night shift, you need to bring it to day shift :)

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; 
}

Please note that this logic assumes that a shift is for 8-12 hours only.

Upvotes: 1

Related Questions