Tell the solution to implement the display of the current work shift on schedule 3-2.

Conditions:

  • Three working days, two days off;
  • Three shifts 1 - from 23:00 (before the current day of the shift, the main day is listed as from 00:00 to 07:00) to 07:00 , 2 - from 07:00 to 15:00 , 3 - from 15:00 to 23:00 ;
  • Total duty shift 5 - "A", "B", "C", "G", "D".

For the beginning, you can take from 01/01/2016 with the following values ​​(B - output):

  • Change "A": В В 1 1 1 В В 3 3 3 В В 2 2 2 (further in a circle);

  • Change “B”: В 3 3 3 В В 2 2 2 В В 1 1 1 В;

  • Change "In": 2 2 2 В 1 1 1 В В 3 3 3 В В;

  • Change “G”: 1 1 В В 3 3 3 В В 2 2 2 В В 1;

  • Change "D": 3 IN 2 2 2 IN 1 1 1 IN 3 3.

There is a solution that determines the number of the shift (including the hour before the current day).

DateNow = new Date(); var time = ("0" + DateNow.getHours()).substr(-2) + ':' + ("0" + DateNow.getMinutes()).substr(-2); window.setTimeout(arguments.callee, 15000); // определяем номер смены, в зависимости от времени var day = DateNow.getDate(); var shift = 1; //shift - смена if (DateNow.getHours() >= 7) { if (DateNow.getHours() >= 15) { if (DateNow.getHours() < 23) shift = 3; else { DateNow.setDate(DateNow.getDate() + 1); day = DateNow.getDate(); } } else shift = 2; } 
  • Check whether you understood correctly: at the entrance - the current time, at the output - the number of the shift and its schedule, starting from the current day. What will correct? - Gleb Kemarsky
  • Yes that's right. I get the shift number, I need a letter. Well, or if in the decision it will be more logical, the shift number (1, 2, 3) and the letter. Thank! - Vasily UK

2 answers 2

Fixed a bug and took into account the rule of 23 hours . Check the result, please.

 // что сейчас var now = new Date(); // сколько полных часов var hour = now.getHours(); // какая сейчас смена var shift = 0; if (hour < 7) { shift = 1; } else if (hour < 15) { shift = 2; } else if (hour < 23) { shift = 3; } else { shift = 1; }; // номер дня с начала года var dayNewYear = new Date(now.getFullYear(), 0, 0); var diff = now - dayNewYear; var dayOne = 1000 * 60 * 60 * 24; var dayYear = Math.floor(diff / dayOne); // номер дня с начала 15-дневного цикла // вычитаем 1, чтобы получить номер от 0 до 14 // если уже 23 часа, то считаем день завтрашним и добавляем 1 var lenShift = 15; var dayShift = (dayYear - 1 + (hour < 23 ? 0 : 1)) % lenShift; // графики групп var group = [ {name: 'А', schedule: 'ВВ111ВВ333ВВ222'}, {name: 'Б', schedule: 'В333ВВ222ВВ111В'}, {name: 'В', schedule: '222ВВ111ВВ333ВВ'}, {name: 'Г', schedule: '11ВВ333ВВ222ВВ1'}, {name: 'Д', schedule: '3ВВ222ВВ111ВВ33'} ]; // какая сейчас группа var groupNum = -1; for (var key in group) { if (group[key].schedule[dayShift] == shift) { groupNum = key; } } // график, начиная с сегодня var scheduleShift = group[groupNum].schedule; scheduleShift += scheduleShift; scheduleShift = scheduleShift.substring(dayShift, dayShift + 15); // проверка //alert('Сегодня ' + now + '. День с начала года ' + dayYear + '. День с начала цикла ' + dayShift + '.'); // результат alert('Смена ' + shift + '. Группа ' + group[groupNum].name + '. График, начиная с сегодняшнего дня ' + scheduleShift + '.'); 

  • Displays - Change 2. Group B. Schedule, starting today 22ВВ111ВВ333ВВ. Now it should be - Change 2. Group A. (the average was yesterday and tomorrow will be) Change "B" was at the very last (last) night. - Vasily UK
  • @VasilyUK Fixed a bug. Check again, please. - Gleb Kemarsky
  • Yes, now displays the desired schedule and shift. I will see how it will be at night (there because of the hour before the day before, when the shift comes, for example, 01/01/2016 at 23:00, and in the schedule it is marked as 02/01/2016) and with the transition to next month. Thanks for the decision !!! - Vasily UK
  • @VasilyUK Ie today at 23:01 you need to show the schedule, as if tomorrow? - Gleb Kemarsky
  • @VasilyUK Took into account the transition in 23 hours. Write whether everything is in order. - Gleb Kemarsky

If you correctly understood the task, it displays: Change: 2 Name of the shift В Schedule: 2 2 2 В В 1 1 1 В В 3 3 3 В

 teams = [ {name: 'A', work: 'В В 1 1 1 В В 3 3 3 В В 2 2 2'}, {name: 'Б', work: 'В 3 3 3 В В 2 2 2 В В 1 1 1 В'}, {name: 'В', work: '2 2 2 В В 1 1 1 В В 3 3 3 В В'}, {name: 'Г', work: '1 1 В В 3 3 3 В В 2 2 2 В В 1'}, {name: 'Д', work: '3 В В 2 2 2 В В 1 1 1 В В 3 3'} ]; DateNow = new Date(); var time = ("0" + DateNow.getHours()).substr(-2) + ':' + ("0" + DateNow.getMinutes()).substr(-2); window.setTimeout(time.callee, 15000); // определяем номер смены, в зависимости от времени var day = DateNow.getDate(); var shift = 1; //shift - смена if (DateNow.getHours() >= 7) { if (DateNow.getHours() >= 15) { if (DateNow.getHours() < 23) shift = 3; else { DateNow.setDate(DateNow.getDate() + 1); day = DateNow.getDate(); } } else shift = 2; } alert('Смена: ' + shift + ' Имя смены ' + teams[shift].name + ' График: ' + teams[shift].work); 
  • no binding to calendar. The list of shifts was given as starting on 01/01/2016. The very frequency of changes is infinite, but lie on the calendar. That is, for now - 04/28/2016 at 12:15 (from 07:00 to 15:00) change "A", 2 (average of three). - Vasily UK