我有根据铁路时间往返的时间,现在我将上午AM转换为正常时间。但是之后,我需要增加30分钟的间隔。我对迭代器方法有点困惑。请提前帮助谢谢

我的密码

let fromtime = '09:00:00'

let totime = '21:00:00'

let getGenTime = (timeString) => {
      let H = +timeString.substr(0, 2);
      let h = (H % 12) || 12;
      let ampm = H < 12 ? " AM" : " PM";
      return timeString = h + timeString.substr(2, 3) + ampm;
}


现在我有上午和下午两种格式的往返时间,但我需要在往返时间之间增加30分钟的间隔

  fromtime = getGenTime(fromtime) // 09:00 AM

  totime =  getGenTime(totime)   //  09:00 PM


控制台中的预期结果:

 09:00 AM
 09:30 AM
 10:00 AM
 10:30 AM
 11:00 AM
 ....
 .....
 08:30 PM
 09:00 PM

最佳答案

如果使用Date对象而不是strings,或者使用moment.js为此提供了许多有用的方法,那会更好。

但是无论如何,它都只需要一个带有一些检查的循环即可实现您想要的,我做了一个函数,该函数将返回一个在fromtimetotime之间所有时间的数组:

function returnTimesInBetween(start, end) {
  var timesInBetween = [];
  var startH = parseInt(start.split(":")[0]);
  var startM = parseInt(start.split(":")[1]);
  var endH = parseInt(end.split(":")[0]);
  var endM = parseInt(end.split(":")[1]);

  if (startM == 30)
    startH++;
  for (var i = startH; i < endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
  }
  timesInBetween.push(endH + ":00");
  if (endM == 30)
    timesInBetween.push(endH + ":30")

  return timesInBetween.map(getGenTime);
}


演示:



let fromtime = '09:00:00'

let totime = '21:00:00'

let getGenTime = (timeString) => {
  let H = +timeString.substr(0, 2);
  let h = (H % 12) || 12;
  let ampm = H < 12 ? " AM" : " PM";
  return timeString = h + timeString.substr(2, 3) + ampm;
}



function returnTimesInBetween(start, end) {
  var timesInBetween = [];

  var startH = parseInt(start.split(":")[0]);
  var startM = parseInt(start.split(":")[1]);
  var endH = parseInt(end.split(":")[0]);
  var endM = parseInt(end.split(":")[1]);

  if (startM == 30)
    startH++;

  for (var i = startH; i < endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
  }

  timesInBetween.push(endH + ":00");
  if (endM == 30)
    timesInBetween.push(endH + ":30")

  return timesInBetween.map(getGenTime);
}

console.log(returnTimesInBetween(fromtime, totime));





编辑:

要从Datefromtime字符串获取totime对象,可以使用Date构造函数:

let fromtime = '09:00:00';
var d = new Date(Date.UTC(2017, 10, 10, parseInt(fromtime.split(":")[0]), parseInt(fromtime.split(":")[1])));


请注意Locale和TimeZones,结果可能会有所不同。

演示:



let fromtime = '09:00:00'

let totime = '21:00:00'

let getGenTime = (timeString) => {
  let H = +timeString.substr(0, 2);
  let h = (H % 12) || 12;
  let ampm = H < 12 ? " AM" : " PM";
  return timeString = h + timeString.substr(2, 3) + ampm;
}



function returnTimesInBetween(start, end) {
  var timesInBetween = [];

  var startH = parseInt(start.split(":")[0]);
  var startM = parseInt(start.split(":")[1]);
  var endH = parseInt(end.split(":")[0]);
  var endM = parseInt(end.split(":")[1]);

  if (startM == 30)
    startH++;

  for (var i = startH; i < endH; i++) {
    timesInBetween.push(i < 10 ? "0" + i + ":00" : i + ":00");
    timesInBetween.push(i < 10 ? "0" + i + ":30" : i + ":30");
  }

  timesInBetween.push(endH + ":00");
  if (endM == 30)
    timesInBetween.push(endH + ":30")

  timesInBetween.map(getGenTime);
  return timesInBetween.map(time => new Date(Date.UTC(2017, 10, 10, parseInt(time.split(":")[0]), parseInt(time.split(":")[1]))));
}

console.log(returnTimesInBetween(fromtime, totime));

07-22 14:17