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
In any case you need to do O(N) operations to enumerate array elements.
However, you could iterate through Date
objects itself.
function timeArr(interval) //required function with custom MINUTES interval
{
var result = [];
var start = new Date(1,1,1,0,0);
var end = new Date(1,1,2,0,0);
for (var d = start; d < end; d.setMinutes(d.getMinutes() + 5)) {
result.push(format(d));
}
return result;
}
function format(inputDate) // auxiliary function to format Date object
{
var hours = inputDate.getHours();
var minutes = inputDate.getMinutes();
var ampm = hours < 12? "AM" : (hours=hours%12,"PM");
hours = hours == 0? 12 : hours < 10? ("0" + hours) : hours;
minutes = minutes < 10 ? ("0" + minutes) : minutes;
return hours + ":" + minutes + " " + ampm;
}
Demo