There is a time array, the time is not continuous, how to complete it better?

A time array, for example:

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"
]

like this, how to achieve the missing time between completions?

Mar.11,2021

your need is to make up the missing hour between two time periods, right? see that your array is in order. It's very simple. You can take out the beginning and tail of the array, then turn it into a timestamp, and then use a for loop. As long as it is less than the trailing timestamp, add an hour all the time, store it in a new array, and finally assemble the new number back into the string to complete.


it seems that this is not a completion problem, but a problem of generating an array of subscript 0 and subscript length-1 at intervals of 1 hour.

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)

it is more convenient to use moment.js .


 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);
      }
      indexPP;
  }
  alert(times);
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1beb2c9-307a0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1beb2c9-307a0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?