-11

Hi I'm trying to generate an array of every hour of the day. However, I want it to start with the current hour.

For example,

if the current time is 2:00 pm, the array should start:

['1400', '1500', '1600', 1700', '1800', '1900', '2000', '2100', '2200', '2300']

instead of

['0000', '0100', '0300', '0400', '0500', '0600', '0700', '0800', '0900', '1000', '1100', '1200', '1300', '1400', '1500', '1600', 1700', '1800', '1900', '2000', '2100', '2200', '2300']

2
  • 9
    If you are trying, where's your code? Commented Mar 12, 2018 at 21:55
  • use .filter with parseInt and (new Date().getHours()) to filter all hours before the current hour of the day.
    – George
    Commented Mar 12, 2018 at 21:57

3 Answers 3

3

Pretty straight-forward. See comments inline:

var result = [];                      // Results will go here
var nowHour = new Date().getHours();  // Get current hour of the day

// Loop from current hour number to 23
for(var i = nowHour; i < 24; i++){
  result.push(i + "00");  // Put loop counter into array with "00" next to it
}

console.log(result); // show results

2
  • Hi @scott-marcus , if I wanted to skip an hour ahead, do you know how I can do that? Let say instead of starting with the current hour, I want to skip 1 hour ahead. For example, it's 5pm, so I want to start the array at ['1800', '1900', '2000', '2100', '2200', '2300']
    – lotsonj
    Commented Mar 13, 2018 at 23:18
  • @lotsonj Yes, that's simple. Instead of for(var i = nowHour; i < 24; i++){, you'd change the loop to: for(var i = nowHour + 1; i < 24; i++){ to simply start one more than the current hour. Commented Mar 13, 2018 at 23:40
2

You can use fill and map of Array.Prototype Something like below.

Array(24-new Date().getHours()).fill().map((e,i)=>i+new Date().getHours());

var currHour=new Date().getHours();
console.log(Array(24-currHour).fill().map((e,i)=>i+currHour+"00"));

3
  • Thank you so much @yajiv, that's exactly what I was looking for. I really appreciate the help.
    – lotsonj
    Commented Mar 12, 2018 at 22:40
  • Hi @yajiv, both you and Scott gave good answers and I really appreciate the help. I saw yours before his. I'm sorry I checked and unchecked your answer. I saw his answer later and just thought his was much closer to what I was looking for with the for loop included. I still appreciated your answer tho and voted it up. I which StackOverflow allowed choosing more than one answer as the best. I would have marked yours as well. Sorry
    – lotsonj
    Commented Mar 12, 2018 at 23:30
  • This is a really inefficient algorithm. It creates numerous unnecessary Date objects, an Array that is essentially just used as a counter and still needs additional processing to add a leading zero to hours before 10:00.
    – RobG
    Commented Mar 13, 2018 at 0:41
0

It can be done reasonably efficiently in a single statement, including leading zeros on hours before 10:00, and generically using Array.from with an optional callback, e.g.

console.log(
  Array.from(Array(24-new Date().getHours()),(x,i)=>('0'+(23-i)).slice(-2)+'00').reverse()
);

Unfortunately it still creates 2 arrays, the first is an empty array of the required length that is then used to generate the second array with the required values, so not much of a performance hit.

If there was a forEvery method that didn't skip non-existant properties in the same way from doesn't, it could be one with just one Array (just like a for loop does).

Not the answer you're looking for? Browse other questions tagged or ask your own question.