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

129
Visualizações
How to get an array of 10 next Mondays in javascript

Here is my code but this array returns only the same elements, i am trying to create an array which contains 10 elements as the next mondays anyone know please help me

my code:

function getNextDayOfTheWeek(dayName, excludeToday = true, refDate = new Date()) {
    const dayOfWeek = ["sun","mon","tue","wed","thu","fri","sat"]
                      .indexOf(dayName.slice(0,3).toLowerCase());
    if (dayOfWeek < 0) return;
    refDate.setHours(0,0,0,0);
    refDate.setDate(refDate.getDate() + +!!excludeToday + 
                    (dayOfWeek + 7 - refDate.getDay() - +!!excludeToday) % 7);
    return refDate;
}
let arr=[]
for(let i=0;i<10;i++){
    arr.push(arr[i]=getNextDayOfTheWeek("Monday",false))
}

console.log(arr)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

getNextDayOfWeek is being given the same inputs and so will have the same outputs.

You need to:

  1. Establish the base line date and pass it as the third parameter
  2. Pass the resulting date on the subsequent calls so that it advances
  3. Indicate that it should exclude the base line date if it matches the requested date
  4. Insert a new Date into the result array, because Dates are objects and thus passed by reference. If you don't do this, each call of getNextDayOfTheWeek will be making changes to the same object stored in each position of the result array.
  5. Note that your local time might cause the displayed date to appear to be before or after the expected date. You will likely need to decide how to account for local timezone (note that the result might show Z at the end of the time indicating UTC).
let arr=[]
let refDate = new Date()
for(let i=0;i<10;i++){
    refDate = getNextDayOfTheWeek("Monday",false, refDate)
    arr.push(refDate)
}

function getNextDayOfTheWeek(dayName, excludeToday = true, refDate = new Date()) {
    const dayOfWeek = ["sun","mon","tue","wed","thu","fri","sat"]
                      .indexOf(dayName.slice(0,3).toLowerCase());
    if (dayOfWeek < 0) return;
    refDate.setHours(0,0,0,0);
    refDate.setDate(refDate.getDate() + +!!excludeToday + 
                    (dayOfWeek + 7 - refDate.getDay() - +!!excludeToday) % 7);
    return refDate;
}
let arr=[]
let refDate = new Date()
for(let i=0;i<10;i++){
    refDate = getNextDayOfTheWeek("Monday", i > 0 ? true : false, refDate)
    arr.push(new Date(refDate))
}

console.log(arr)

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