Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

117
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!