获取开始时间和结束时间之间的时间段

时间:2016-01-13 13:18:27

标签: jquery

我有开始时间结束时间时间间隔

我必须在开始时间和结束时间之间获得时间段。

我有

var starttime = "09:50:00";
var endtime = "10:50:00";
var interval = "10";

我希望time slots作为数组

var timeslots = ["09:50:00","10:00:00","10:10:00","10:20:00","10:30:00","10:40:00","10:50:00"];

starttimeendtimeinterval可能会有所不同。如何根据需要获得时间段?

添加时间间隔我有:

 function addMinutes(time, minsToAdd) {
  function D(J){ return (J<10? '0':'') + J;};
  var piece = time.split(':');
  var mins = piece[0]*60 + +piece[1] + +minsToAdd;

  return D(mins%(24*60)/60 | 0) + ':' + D(mins%60);  
}  //this is working

我在下面尝试生成time slot数组。

var starttime = "09:50:00";
var interval = "10";
var endtime = "10:50:00";
var timeslots =[stime];


while(starttime ==endtime ){

starttime = addMinutes(starttime , interval); 
timeslots.push(stime);

}
alert(timeslots.length);

但是数组长度显示为1,意味着无效...

请帮我纠正这个..或者用任何其他方式帮助我...

3 个答案:

答案 0 :(得分:2)

我无法使你的addMinutes功能正常工作(chrome一直没有响应)所以我实现了自己的功能。

我没有看到你宣布stime的地方,但我改变了对starttime的兴趣并且还将starttime == endtime更改为starttime!= endtime并且代码正常工作。

function addMinutes(time, minutes) {
  var date = new Date(new Date('01/01/2015 ' + time).getTime() + minutes * 60000);
  var tempTime = ((date.getHours().toString().length == 1) ? '0' + date.getHours() : date.getHours()) + ':' +
    ((date.getMinutes().toString().length == 1) ? '0' + date.getMinutes() : date.getMinutes()) + ':' +
    ((date.getSeconds().toString().length == 1) ? '0' + date.getSeconds() : date.getSeconds());
  return tempTime;
}

var starttime = "09:50:00";
var interval = "10";
var endtime = "10:50:00";
var timeslots = [starttime];


while (starttime != endtime) {

  starttime = addMinutes(starttime, interval);
  timeslots.push(starttime);

}
alert(timeslots);

DEMO:

https://jsfiddle.net/vay9rL7g/

<强>结果:

09:50:00,10:00:00,10:10:00,10:20:00,10:30:00,10:40:00,10:50:00

答案 1 :(得分:2)

示例输入:

var startTime = "10:00",
    endTime = "17:00",
    interval = 30;

示例输出:

["10:00", "10:30", "11:00", "11:30", "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30", "16:00", "16:30", "17:00"]

源代码:

function parseTime(s) {
      var c = s.split(':');
      return parseInt(c[0]) * 60 + parseInt(c[1]);
    }

    function convertHours(mins){
      var hour = Math.floor(mins/60);
      var mins = mins%60;
      var converted = pad(hour, 2)+':'+pad(mins, 2);
      return converted;
    }

    function pad (str, max) {
      str = str.toString();
      return str.length < max ? pad("0" + str, max) : str;
    }

    function calculate_time_slot(start_time, end_time, interval = "30"){
        var i, formatted_time;
      var time_slots = new Array();
        for(var i=start_time; i<=end_time; i = i+interval){
        formatted_time = convertHours(i);
        time_slots.push(formatted_time);
      }
      return time_slots;
    }

    var startTime = "10:00";
    var endTime = "17:00";

    var start_time = parseTime(startTime),
        end_time = parseTime(endTime),
        interval = 30;

    var times_ara = calculate_time_slot( start_time, end_time, interval );

    console.log(times_ara);

JS小提琴链接:Check JS fiddle

答案 2 :(得分:0)

&#13;
&#13;
$(function(){
  var starttime = "09:50:00";
  var endtime = "10:50:00";
  var interval = "10";
  var startTimeInMin = convertToMin(starttime);
  var endTimeInMin = convertToMin(endtime);

  timeIntervel = parseInt(endTimeInMin-startTimeInMin)/(parseInt(interval)*60);

  nextSlot = starttime;

  slotArra = [nextSlot];
  for(i=0; i<timeIntervel; i++){
    var nextSlot =  getNextSlot(nextSlot, interval);
    slotArra.push(nextSlot);
  }

  alert(slotArra)

  function convertToMin(inputTime){
    inputTime = inputTime.split(':');
    var hrinseconds = parseInt(inputTime[0])*3600;
    var mininseconds = parseInt(inputTime[1])*60;
    return parseInt(hrinseconds)+parseInt(mininseconds)+parseInt(inputTime[2])
  }

  function getNextSlot(starttime, interval) {
    var piece = starttime.split(':');
    var hour = piece[0];
    var minutes = piece[1];
    var seconds = piece[2];
    var newMinute = parseInt(minutes)+parseInt(interval)
    if(newMinute>=60){
      hour = parseInt(hour)+1;
      newMinute = newMinute%60
      if(newMinute==0)
        newMinute = '00';
    }
    var newTim = hour+':'+newMinute+':'+seconds;
    return newTim;
  }	
})
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;