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

174
Visualizações
Check date range with an array

I have an array, in which I have inserted some day that are holidays.

In my app I choose an interval (startDate and endDate) and I should check that this interval is not in my array. How can I do?

For example:

holidaysArray = [
"2020-04-12",
            "2020-04-13",
            "*-03-17",
            "*-05-01",
            "*-06-02"
}

I choose as

startDate:  2022-03-15T16:16:00.000Z 
endDate:2022-03-18T16:12:23.103Z

What I expect

So now I should count the day between the startDate and EndDate, and the result is 4 days. But I my array there is value "*-03-17", so I should not considering a day.

How can I do?

I have a datepicker:

<DatePicker
            modal
            open={showStartDate}
            date={startDate}
            title={null}
            confirmText="Ok"
            cancelText="Cancel"
            mode="date"
            onConfirm={(date) => {
              setShowStartDate(false)
              onChangeStart( date)
            }}
            onCancel={() => setShowStartDate(false)}
          />

<DatePicker
            modal
            open={showEndDate}
            date={endDate}
            title={null}
            confirmText="Ok"
            cancelText="Cancel"
            mode="date"
            onConfirm={(date) => {
              setShowEndDate(false)
              onChangeStart( date)
            }}
            onCancel={() => setShowEndDate(false)}
          />

So my

onChangeStart = (selectedDate) => {
   checkDate(selectedDate, endDate)
}

checkDate = (start, end) => {
  const holidayArray = travelInfo.time.holidayArray
  //
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

When you iterate over 'holidayArray' you need to check if the holiday falls between the selected date, if it does just decrement the total difference between the dates by '1' (assuming you are counting Sundays' as well). For the case where the dates have *, you need to find the 'years' that can fall between the dates and use them to replace the * to form the appropriate date.

First Step (Find year (or years if the selection is too large, just in case))

(assuming endDate > startDate)

const findYears = (startDate, endDate) => {
    let yearsArray = [];
    let endYear = parseInt(endDate?.split("-")[0]);
    let startYear= parseInt(startDate?.split("-")[0]);
    yearsArray.push(endYear);
    while(endYear !== startYear){
         endYear--;
         yearsArray.push(endYear);
    }
  return yearsArray;
}

Now complete your checkDate function

checkDate = (start, end) => {
    const years = findYears(start, end)
    const holidayArray = travelInfo.time.holidayArray;
    var from = new Date(start);
    var to = new Date(end);
    var diff = 0;
    holidayArray.forEach(holiday => {
        if (holiday.match("\\*")) {
            years.forEach(year => {
                var check = new Date(holiday.replace("\\*", year));
                if (check > from && check < to) {
                    diff--
                }
            })
        } else {
            var check = new Date(holiday);
            if (check > from && check < to) {
                diff--
            }
        }
    })
    const diffTime = Math.abs(start - end);
    var diffDays = Math.round(diffTime / (1000 * 60 * 60 * 24));
    return diffDays + diff
}
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