DEV Community

Damjan Dimitrov
Damjan Dimitrov

Posted on

Short oneliners: Create array of hour interval strings

This is a way to get an array of hours in a day in the HH:mm format:

Array.from({ length: 24 }, (_, k) => k + 6).map(el => `${String(el % 24).padStart(2, '0')}:00`);
Enter fullscreen mode Exit fullscreen mode

Result:

Array(24) ['06:00', '07:00', '08:00', '09:00', '10:00', '11:00', '12:00', '13:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00', '21:00', '22:00', '23:00', '00:00', '01:00', '02:00', '03:00', '04:00', '05:00']
Enter fullscreen mode Exit fullscreen mode

We can change the start time by changing the "k + 6" increment amount, or also include the same starting hour in the next day by changing the result length to 25.

Top comments (0)