我有来自铁路时间的时间,现在我转换成正常时间AM到PM。但之后我需要增加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
答案 0 :(得分:1)
如果您使用Date
对象而不是strings
,或使用moment.js
来提供许多有用的方法,那会更好。
但是任何方式它只需要一个带有一些检查的循环来实现你想要的东西,我创建了一个函数,它会在你的fromtime
和totime
之间返回一个数组:
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));
&#13;
修改强>
要从Date
和fromtime
字符串中获取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));
&#13;