JavaScript以块的形式分割时间

时间:2011-08-09 16:02:00

标签: javascript datetime

如果我知道开始时间和结束时间,我怎么能每隔X分钟分配时间。例如,如果我的开始时间是13:00而我的结束时间是:15:00并且每30分钟拆分一次,那么我想得到一个包含以下内容的数组:

13:00 - 13:30
14:00 - 14:30
14:30 - 15:00

4 个答案:

答案 0 :(得分:1)

您可以使用datejs及其add方法为Date对象添加分钟。 compareTo方法可用于检查您是否仍在适当的范围内。

如果您不想使用外部库,可以参考w3schools

//Set minutes
var myDate=new Date();
myDate.setMinutes(myDate.getMinutes()+30);

//Compare two dates.
var x=new Date();
x.setFullYear(2100,0,14);
var today = new Date();
if (x>today)
  {
  alert("Today is before 14th January 2100");
  }
else
  {
  alert("Today is after 14th January 2100");
  }

答案 1 :(得分:1)

13:00 - 13:30
13:30 - 14:00
14:00 - 14:30
14:30 - 15:00

see it in action

var makeTimeIntervals = function (startTime, endTime, increment) {
    startTime = startTime.toString().split(':');
    endTime = endTime.toString().split(':');
    increment = parseInt(increment, 10);

    var pad = function (n) { return (n < 10) ? '0' + n.toString() : n; },
        startHr = parseInt(startTime[0], 10),
        startMin = parseInt(startTime[1], 10),
        endHr = parseInt(endTime[0], 10),
        endMin = parseInt(endTime[1], 10),
        currentHr = startHr,
        currentMin = startMin,
        previous = currentHr + ':' + pad(currentMin),
        current = '',
        r = [];

    do {
        currentMin += increment;
        if ((currentMin % 60) === 0 || currentMin > 60) {
            currentMin = (currentMin === 60) ? 0 : currentMin - 60;
            currentHr += 1;
        }
        current = currentHr + ':' + pad(currentMin);
        r.push(previous + ' - ' + current);
        previous = current;
  } while (currentHr !== endHr);

    return r;
};

var a = makeTimeIntervals('13:00', '15:00', 30);

for (var i in a) if (a.hasOwnProperty(i)) { document.body.innerHTML += a[i] + '<br />'; }

答案 2 :(得分:0)

很好地破坏了一切:P ......哦......对于想要为字符串做这个的人来说:

function getIntervals(startString,endString,intervalString) {
var start = startString.split(":");
var end = endString.split(":");
var interval = intervalString.split(":");
startInMinutes = start[0]*60+start[1]*1;
endInMinutes = end[0]*60+end[1]*1;
intervalInMinutes = interval[0]*60+interval[1]*1;
var times = [];
var intervalsOfTime = [];

for(var i = startInMinutes; i <= endInMinutes; i+=intervalInMinutes) {
  var hour = Math.floor(i/60) + "";
  var minute = i%60 + "";
  minute = (minute.length < 2) ? "0" + minute : minute;
  hour = (hour.length < 2) ? "0" + hour : hour;
  times.push( hour + ":" + minute);
}


for(var i = 0; i < times.length-1; i++)
  intervalsOfTime.push(times[i] + " - " + times[i+1])

return intervalsOfTime;

}

答案 3 :(得分:0)

我知道我在回答这个问题上很晚,但是我将解决方案发布给仍然陷于困境的任何人。

使用moment.js:-

const moment = require('moment');

// Actual logic

const splitTime = (startTime, endTime, interval) =>{
    const result = [startTime.toString()];
    let time = startTime.add(interval,'m');
    while(time.isBetween(startTime,endTime,undefined,[])){
        result.push(time.toString());
        time = time.add(interval,'m');
    }
    return result;
}


// You change these values according to your needs

const interval = 60;
const startTime = new moment({hour:00,minute:00});
const endTime = new moment({hour:12, minute:00});

const timeSlices = splitTime(startTime,endTime,interval); 

// For printing out the intervals 

for(let i=0;i<timeSlices.length-1;i++){
    console.log(timeSlices[i]+" - "+timeSlices[i+1]);
}

希望这是一个简单的答案,对读者有帮助。 :)