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

118
Visualizações
How to display time range on iteration?

I am trying to run a for loop that has to loop 8 times. On each iteration I want the loop to increment the value by +1 hour.

The final output should be in this time format:

opening hours: 08:00, 09:00, 10:00,11:00, 12:00, 13:00, 14:00, 15:00,16:00

In this case timediff mentioned in the for...loop holds the value of 8.

var minutesToAdd = 60;
var currentDate = new Date("2022-04-10 08:00:00");
var futureDate = new Date(currentDate.getTime() + minutesToAdd * 120000).toLocaleTimeString();

for (let i = 0; i < timeDiff; i++) {
  console.log(futureDate, 'futureeee date');
}

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

0

Your code never updates futureDate in the loop. Also, when you expect to print 08:00 in the example, you should print currentDate when no minutes have been added yet.

I would also suggest you use the native function setMinutes and getMinutes to add a number of minutes to a date object.

You say that timeDiff has a value of 8, but then you say you want 9 outputs (8:00 ... 16:00), so you'll need an additional iteration.

Finally, to get the hh:mm output format, there are several solutions. One is to choose a locale that uses a format that is close to what you need, and express you want the short format (without seconds):

let timeDiff = 9; // One more to also output 16:00
let minutesToAdd = 60;
let currentDate = new Date("2022-04-10 08:00:00");

for (let i = 0; i < timeDiff; i++) {
  console.log(currentDate.toLocaleTimeString("en-SE", { timeStyle: "short" }));
  currentDate.setMinutes(currentDate.getMinutes() + minutesToAdd);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You can then use a simple for-loop to iterate from 0 up to and including timeDiff, incrementing each iteration by 1.

for (let hours = 0; hours <= timeDiff; hours += 1) {
  // ...
}

Instead of a starting date we'll start with a timestamp Date.parse("2022-04-10 08:00:00"). This allows us to add the time from the loop to the timestamp and create a new date.

const date = new Date(currentTimestamp + hours*HOURS);

To only display the time I've taken the liberty of using the solution provided by the answer of trincot.

const MILLISECOND = 1                , MILLISECONDS = MILLISECOND;
const SECOND      = 1000*MILLISECONDS, SECONDS      = SECOND     ;
const MINUTE      = 60*SECONDS       , MINUTES      = MINUTE     ;
const HOUR        = 60*MINUTES       , HOURS        = HOUR       ;

const timeDiff = 8;
var currentTimestamp = Date.parse("2022-04-10 08:00:00");

for (let hours = 0; hours <= timeDiff; hours += 1) {
  const date = new Date(currentTimestamp + hours*HOURS);
  console.log(date.toLocaleString(undefined, { timeStyle: "short" }));
}

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