I\'m trying to create an array of times (strings, not Date objects) for every X minutes throughout a full 24 hours. For example, for a 5 minute interval the arr
Loops are unnecessary in this case.
//Array.from, only supported by Chrome 45+, Firefox 32+, Edge and Safari 9.0+
//create an array of the expected interval
let arr = Array.from({
length: 24 * 60 / 5
}, (v, i) => {
let h = Math.floor(i * 5 / 60);
let m = i * 5 - h * 60;
//convert to 12 hours time
//pad zero to minute
if (m < 10) {
m = '0' + m;
}
let label = 'AM';
if (h > 12) {
label = 'PM';
h -= 12;
}
if (h === 0) {
h = 12;
}
return h + ':' + m + ' ' + label;
});
document.body.textContent = JSON.stringify(arr);
var arr = Array.apply(null, {
length: 24 * 60 / 5
}).map(function(v, i) {
var h = Math.floor(i * 5 / 60);
var m = i * 5 - h * 60;
if (m < 10) {
m = '0' + m;
}
var label = 'AM';
if (h > 12) {
label = 'PM';
h -= 12;
}
if (h === 0) {
h = 12;
}
return h + ':' + m + ' ' + label;
});
document.body.textContent = JSON.stringify(arr);