3 回答
TA贡献1802条经验 获得超6个赞
这看起来,不是一个补全的题,是按照间隔1小时生成下标0和下标length-1的时间间隔数组的问题。
let times = [
'2018-5-15 00:00:00', '2018-5-15 01:00:00', '2018-5-15 02:00:00', '2018-5-15 08:00:00', '2018-5-16 03:00:00'
]
let oneHour = 60 * 60 * 1000;
let now = new Date(times[0]);
let max = new Date(times[times.length - 1]);
let result = [];
while (!(max < now)) {
result.push(now)
now = new Date(now.getTime() + oneHour);
}
console.log(result)
还是用moment.js比较方便。
TA贡献1856条经验 获得超5个赞
TA贡献1824条经验 获得超6个赞
var times = [
'2018-5-15 00:00:00','2018-5-15 01:00:00','2018-5-15 02:00:00','2018-5-15 08:00:00','2018-5-16 03:00:00'];
var oneHour = 60*60*1000;
var index = 0;
while(index<times.length-1){
var next = new Date(new Date(times[index]).getTime()+oneHour);
if(next<new Date(times[index+1])){
times.splice(index+1,0,next);
}
index++;
}
alert(times);
添加回答
举报