gpt4 book ai didi

javascript - 如何在 JavaScript 中循环时间值?

转载 作者:行者123 更新时间:2023-12-02 15:02:22 25 4
gpt4 key购买 nike

我遇到一种情况,用户选择开始、结束时间和 session 长度。一旦选择了开始时间和 session 长度,我必须从开始时间一直循环到下午 5:00,并增加 session 长度并在结束时间输出每个时间段。我的代码在 javascript 中使用 Date API,问题是我的 for 循环只给我第一个值,然后停止。我不确定我的代码有什么奇怪的,所以如果有人可以帮助,请告诉我。这是我的代码:

<tr>
<th>Start Time:</th>
<td>
<select name="stime" id="stime" />
<option value="">--Select start time--</option>
</select>
</td>
<br />

<th>Meeting Length:</th>
<td>
<select name="meet_leng" id="meet_leng">
<option value="">--Select length--</option>
</select>
</td>
<br />

<th>End Time:</th>
<td>
<select name="etime" id="etime"/>
<option value="">--Select end time--</option>
</select>
</td>
</tr>

Javascript:

$(function() {
//This loop create values fro meeting length
for(var i=5; i <= 60; i+=5){
$('#meet_leng').append('<option value="'+i+'">'+i+' min'+'</option>')
}


for(var i=700; i<= 1700; i+=15){
var mins = i % 100;
var hours = parseInt(i/100);

if (mins > 45) {
mins = 0;
hours += 1;
i = hours * 100;
}

var AmPm = " AM";
//var standardTime = hours > 11 ? ' PM' : ' AM';
//format hours
if(hours == 12){
AmPm = " PM";
}

if (hours > 12) {
hours = hours - 12;
AmPm = " PM";
}

$('#stime').append('<option value="'+('0' + (hours)).slice(-2)+':'+('0' + mins).slice(-2)+AmPm+'">'+('0' + (hours)).slice(-2)+':'+('0' + mins).slice(-2)+AmPm+' </option>')
}
});

$('#meet_leng').on('change', function() {
var time1 = new Date();
var time2 = new Date();
var add = new Date();

//meeting length
var meetingLength = parseInt($('#meet_leng').val());

//start time
var startTime = $('#stime').val();
var startHour = startTime.split(':')[0];
var startMin = startTime.split(':')[1].replace(/AM|PM/gi,'');

//end time
var endTime = '05:00 PM';
var endHour = endTime.split(':')[0];
var endMin = endTime.split(':')[1].replace(/AM|PM/gi,'');

//Check if start time is PM and adjust hours to military
if(startTime.indexOf('PM') > -1){
startHour = parseInt(startHour) + 12;
console.log(startHour);
alert(startHour)
}

//Check if end time is PM and adjust hours to military
if(endTime.indexOf('PM') > -1){
endHour = parseInt(endHour) + 12;
console.log(endHour);
}

//Date API start time
time1.setHours(parseInt(startHour));
time1.setMinutes(parseInt(startMin));

//Date API end time
time2.setHours(parseInt(endHour));
time2.setMinutes(parseInt(endMin));

//Adding meeting length to start time, this value will be use for end time
time1.setMinutes(time1.getMinutes() + meetingLength);

for(var i=time1; i <= time2; i+=meetingLength){
$('#etime').append('<option value="'+i+'">'+i+'</option>')
}

});

这是我的代码的工作示例:https://jsfiddle.net/dmilos89/6n458ze9/6/ .

最佳答案

如果你用这个替换你的 for 循环,你将得到 17:00 之前结束的所有时间

do {
time1.setMinutes(time1.getMinutes() + meetingLength);
$('#etime').append('<option value="' + time1 + '">' + time1 + '</option>');
} while (time1 < time2)

time1.setMinutes(time1.getMinutes() + meetingLength);

while (time1 < time2){
$('#etime').append('<option value="' + time1 + '">' + time1 + '</option>');
time1.setMinutes(time1.getMinutes() + meetingLength);
{

其中一个有效,但第二个也会过滤掉任何立即超过 17:00 的 session

for 循环中的 += 只是将 session 长度添加到字符串表示形式的末尾。

关于javascript - 如何在 JavaScript 中循环时间值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35341689/

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