gpt4 book ai didi

javascript - 两次之间添加 30 分钟间隔

转载 作者:行者123 更新时间:2023-12-03 02:56:51 24 4
gpt4 key购买 nike

我有基于铁路时间的往返时间,现在我转换为正常时间上午到下午。但之后我需要添加 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为此提供了许多有用的方法。

但无论如何,它只需要一个循环和一些检查来实现你想要的,我做了一个函数,它将返回 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));

编辑:

获取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])));

请注意区域设置和时区,您可能会发现结果中有一些小时的差异。

演示:

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));

关于javascript - 两次之间添加 30 分钟间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47583100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com