Check if working time is in the chosen time range Jquery/ Javascript

Asked

Viewed 554 times

0

How to check if the schedule_working is within the chosen range? Example: a bar runs from 08:00 to 13:00, a user has chosen the time interval from 12:00 to 21:00, such as checking that this bar is open in the chosen time range?

var intervalo_escolhido = [12,21];
var horario_funcionamento = [08,13];
  • I need to perform the test with the 2 working hours values.

  • It is the same question as the previous one, simply have to use the solution presented twice.

  • Or use a for or foreach if the working hour vector has a variable size

2 answers

0


verificaHorario(7, 16);

function verificaHorario(horario_inicial, horario_final) {
  var horario_funcionamento = [08, 13];
  for (var horario = horario_inicial; horario <= horario_final; horario++) {
    if (horario < horario_funcionamento[0] || horario >= horario_funcionamento[1]) {
      alert("Horario indisponivel dentro do intervalo escolhido: " + horario);
    } else {
      alert("Horario disponivel dentro do intervalo escolhido: " + horario);
    }
  }
}

I hope I have understood the question and helped. If not, let me know that I try again to help you.

  • 1

    In this example Voce passed was to show that this within the working schedule, ex: it runs from 08:00 to 13:00 and Voce passed the interval from 07:00 to 12:00...at a certain time was to be in working time.

  • Oh yes, you want to check throughout the range. Take a look at the answer now as it is showing available and unavailable times within the chosen range. I hope I have helped.

0

Using this data you have placed you can check by the index of the arrays:

function VerificaRange(range){
  var horario_funcionamento = [08,13];
  if (range[0] >= horario_funcionamento[0] && range[1] <= horario_funcionamento[1]){
    return true;
  }
  else{
    return false;
  }      
}

var intervalo_escolhido = [12,21];
//return false;
var insideRange = VerificaRAnge(intervalo_escolhido);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.