如何在JavaScript中循环时间值?

时间:2016-02-11 14:15:14

标签: javascript jquery for-loop time

我有一个用户选择开始,结束时间和会议长度的情况。一旦选择了开始时间并且会议长度,我必须从START时间一直循环到下午5:00,增加会议长度并在END时间输出每个时隙。我的代码在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/

1 个答案:

答案 0 :(得分:2)

如果你用这个替换你的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的会议

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