Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

125
Visualizações
Validate string time range

I need to create a function to validate the following situation:

  1. Monitored interval inside opening hours: Valid

  2. Monitored interval outside opening hours: Invalid

There is another possibility that the allowed starting range is 23:00 to 04:00. In this case, ranges from 5:00 to 6:00 and 21:00 to 22:00 will not be allowed.

Currently my code validates the day normally, but the crossed day does not.

export default function isOutRangeStringTime({
  initialTime,
  finalTime,
  allowedInitialTime,
  allowedFinalTime,
}) {
  if (allowedInitialTime === allowedFinalTime
    || allowedInitialTime >= allowedFinalTime
    || initialTime >= finalTime) {
    return false;
  }
  return initialTime < allowedInitialTime || finalTime > allowedFinalTime;
}

Here's a picture of the situation that my code doesn't work.

  1. crossed day Valid: Crossday Valid

  2. crossed day invalid: enter image description here

I need validation to work for cross days too.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can consider adding 24h to allowedFinalTime when allowedFinalTime < allowedInitialTime

Consider using Date Objects. For the sake of simplicity this example work with numbers:

function isOutRangeStringTime(
  initialTime,
  finalTime,
  allowedInitialTime,
  allowedFinalTime,
) {
  if (allowedInitialTime === allowedFinalTime
    || initialTime === finalTime) {
    return false;
  }
 if( allowedInitialTime > allowedFinalTime ) {
     allowedFinalTime += 24
     initialTime = (initialTime < allowedInitialTime) ? initialTime + 24 : initialTime;
     finalTime = (finalTime < allowedInitialTime) ? finalTime + 24 : finalTime;
 }

    return initialTime > allowedInitialTime && finalTime < allowedFinalTime;
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda