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
Allocating the resulting array to avoid the overhead of push, parameter validation and locale specifics notwithstanding:
function generate_series(step) {
const dt = new Date(1970, 0, 1);
const rc = [];
while (dt.getDate() === 1) {
rc.push(dt.toLocaleTimeString('en-US'));
dt.setMinutes(dt.getMinutes() + step);
}
return rc;
}
Here's a demo snippet.
function generate_series(step) {
const dt = new Date(1970, 0, 1);
const rc = [];
while (dt.getDate() === 1) {
rc.push(dt.toLocaleTimeString('en-US'));
dt.setMinutes(dt.getMinutes() + step);
}
return rc;
}
function on_generate_series(step) {
const dt = new Date(1970, 0, 1);
const el = document.getElementById("series")
while (el.firstChild)
el.removeChild(el.firstChild);
const series = generate_series(step);
while (series.length > 0) {
let item = document.createElement("div");
item.innerText = series.shift();
el.appendChild(item);
}
}
24 Hour Minute Series