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

202
Visualizações
Javascript programmatic way to get time in 30 min interval between two date objects

Apologies if the question was worded poorly, what I'm looking for is something like this; say you are given two dates (but all you really care about is the time portion of the date object, not the date), 8 AM and 5 PM, I want to be able to print out the time every 30 mins between the two dates (i.e 8:00AM, 8:30,AM 9:00AM, 9:30AM... 4:30PM, 5PM). Replies are appreciated.

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

0

You can store the start date and end date in a Date object, then use a while loop to continuously add 30 minutes to the start while the start date is smaller than the end date.

const start = new Date();
start.setHours(8, 0, 0); //8 AM
const end = new Date();
end.setHours(17, 0, 0); //5 PM

while (start <= end) {
  console.log(start.toLocaleString('en-US', {hour: '2-digit', minute: '2-digit'}));
  start.setMinutes(start.getMinutes() + 30);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

If you'd like to perform some sort of operation or run a function (such as logging out the time) every 30 minutes between a range of times, you can use what is known as cron format in combination with a module that can ingest cron values and run functions on a pre-defined schedule.

I suggest the node-cron module.

To run a task every 30 minutes between 8AM and 5PM, in this case printing out the current time, you can create a schedule using cron formatting.

The cron format for every 30 minutes between 8AM and 5PM would look like */30 08-17 * * *.

const cron = require('node-cron');

cron.schedule('*/30 08-17 * * *', () => {
  console.log(new Date().toLocaleTimeString());
});
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