我做到了
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));
我接下来的5个小时是几个小时。
现在是16:30
(10) ["16:35", "16:40", "16:45", "16:50", "16:55", "17:0", "17:5", "17:10", "17:15", "17:20"]
如何调整我的代码以在5分钟的间隔内获得最后几个小时?
现在是16:30
,我想得到这些
(10) ["16:25", "16:20", "16:15", "16:10", "16:05", "16:0", "15:55", "15:45", "15:35", "15:35"]
答案 0 :(得分:1)
要后退而不是前进,请将+ interval
更改为- interval
。要在过去(而不是未来)中四舍五入到最近的5分钟,请在将setMinutes()
推入数组之前进行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));
<Products>
<Product ref="apple">
<Type>FRUIT</Type>
<Price>100</Price>
</Product>
<Product ref="cabbage">
<Type>VEGETABLE</Type>
<Price>200</Price>
</Product>
</Products>