Reputation: 58662
I did this
var date, array = [];
date = new Date();
while (date.getMinutes() % interval !== 0) {
date.setMinutes ( date.getMinutes() + 1 );
}
// A whole day has 24 * 4 quarters of an hour
// Let's iterate using for loop
for (var i = 0; i < 24 * (60/interval); i++) {
array.push(date.getHours() + ':' + date.getMinutes());
date.setMinutes ( date.getMinutes() + interval);
}
console.log(array.slice(0,10));
I got the next hours in the interval of 5 mins.
Right now is 16:30
(10) ["16:35", "16:40", "16:45", "16:50", "16:55", "17:0", "17:5", "17:10", "17:15", "17:20"]
How do I adjusy my code to get the last hours in the interval of 5 mins ?
Right now is 16:30
, I want to get these
(10) ["16:25", "16:20", "16:15", "16:10", "16:05", "16:0", "15:55", "15:45", "15:35", "15:35"]
Upvotes: 0
Views: 41
Reputation: 21672
To move backwards instead of forwards, change your + interval
to - interval
. To start rounded to the nearest 5 minutes in the past instead of the future, do your setMinutes()
before pushing it to the array.
date.setMinutes(date.getMinutes() - interval);
array.push(date.getHours() + ':' + date.getMinutes());
var date, array = [];
date = new Date();
const interval = 5;
while (date.getMinutes() % interval !== 0) {
date.setMinutes(date.getMinutes() + 1);
}
// A whole day has 24 * 4 quarters of an hour
// Let's iterate using for loop
for (var i = 0; i < 24 * (60 / interval); i++) {
date.setMinutes(date.getMinutes() - interval);
array.push(date.getHours() + ':' + date.getMinutes());
}
console.log(array.slice(0, 10));
Upvotes: 1