假设我有一个如下所示的数组:
{
meeting: 'one',
start_time: "07:15:00",
end_time: "08:15:00"
},
{
meeting: 'two',
start_time: "15:45:00",
end_time: "18:15:00"
}
现在我想计算从00:00:00到24:00:00的所有空闲时间。
例如,新数组将如下所示:
{
meeting: 'free time',
start_time: "00:00:00",
end_time: "07:14:00"
},
{
meeting: 'one',
start_time: "07:15:00",
end_time: "08:15:00"
},
{
meeting: 'free time',
start_time: "08:16:00",
end_time: "15:44:00"
},
{
meeting: 'two',
start_time: "15:45:00",
end_time: "18:15:00"
}
关于如何在没有插件的情况下完成此任务的任何建议?
答案 0 :(得分:2)
function meeting(meeting, time1, time2) {
this.meeting = meeting;
this.start_time = time1;
this.end_time = time2;
}
meeting.prototype.convert = function (a) {
var b = this[a + '_time'];
var start = b.slice(0, 3);
var min = parseInt(b.slice(3, 5));
min = a == "start" ? min - 1 : min + 1;
min = min < 10 ? "0" + min : min;
var ending = b.slice(5, 8);
return start + min + ending
}
Array.prototype.getFreeTime = function () {
var l = this.length,
withFreeTime = [],
i = 0;
while (i < l) {
var s = this[i - 1] ? this[i - 1].convert('end') : "00:00:00";
withFreeTime.push(new meeting('freetime', s, this[i].convert('start')));
withFreeTime.push(this[i]);
i++;
}
//to add the freetime from "18:16:00" to "23:59:00":
withFreeTime.push(new meeting('freetime', this[i-1].convert('end'),'23:59:00'));
return withFreeTime;
}
var meetings = [];
meetings[0] = new meeting('meeting1', "07:15:00", "08:15:00");
meetings[1] = new meeting('meeting2', "15:45:00", "18:15:00");
meetings = meetings.getFreeTime();
console.log(JSON.stringify(meetings));
的console.log:
[{"meeting":"freetime","start_time":"00:00:00","end_time":"07:14:00"},
{"meeting":"meeting1","start_time":"07:15:00","end_time":"08:15:00"},
{"meeting":"freetime","start_time":"08:16:00","end_time":"15:44:00"},
{"meeting":"meeting2","start_time":"15:45:00","end_time":"18:15:00"},
{"meeting":"freetime","start_time":"18:16:00","end_time":"23:59:00"}]
答案 1 :(得分:1)
只是一个简单的例子
var schedule = [{
meeting: 'free time',
start_time: "00:00:00",
end_time: "07:14:00"
},
{
meeting: 'one',
start_time: "07:15:00",
end_time: "08:15:00"
},
{
meeting: 'free time',
start_time: "08:16:00",
end_time: "15:44:00"
},
{
meeting: 'two',
start_time: "15:45:00",
end_time: "18:15:00"
}]
var st,
se,
fh, fm, fs,
ft = 0;
for (var i = 0; i < schedule.length; i++) {
var s = schedule[i];
if (s['meeting'] === 'free time') {
st = s['start_time'].replace(/(\d+):(\d+):(\d+)/, function(t,h,m,s) {
return (h * 3600) + (m * 60) + (+s);
// s is a string, +s converts it to an integer
})
et = s['end_time'].replace(/(\d+):(\d+):(\d+)/, function(t,h,m,s) {
return (h * 3600) + (m * 60) + (+s);
})
/* your free time in seconds for this slot */
ft += (et-st);
}
}
fh = ~~(ft / 3600);
fm = ~~((ft - (fh * 3600)) / 60);
fs = ft - (fh * 3600) - (fm * 60)
console.log("you have %s:%s:%s free time", fh, fm, fs);
// you have 14:42:0 free time
答案 2 :(得分:1)
这是一种可能的解决方案(JSFiddle here):
var time = [{
meeting: 'one',
start_time: "07:15:00",
end_time: "08:15:00"
}, {
meeting: 'two',
start_time: "15:45:00",
end_time: "18:15:00"
}];
// out contains the result
// timeline is a list of minutes containing some event
var out = [], timeline = [];
// Add free time after the last meeting:
// timeline[23*60] = undefined;
// fill up the timeline
for (var m = 0, current = 0; m < 24 * 60 && current < time.length; m += 1) {
var start = str_as_min(time[current].start_time);
var end = str_as_min(time[current].end_time);
if (m >= start) {
if (m <= end) {
timeline[m] = time[current].meeting;
} else {
current += 1;
}
}
}
// fill up the out
for (var m = 0, last = null; m < timeline.length; m += 1) {
var current = timeline[m];
if (current !== last) {
if (out.length > 0) {
out[out.length-1].end_time = min_as_str(m-1);
}
out.push({
start_time: min_as_str(m),
meeting: timeline[m] === undefined ? 'free_time' : timeline[m]
});
}
if (m === timeline.length - 1) {
out[out.length-1].end_time = min_as_str(m);
}
last = current;
}
console.log(JSON.stringify(out));
// helper functions
function min_as_str (min) {
return Math.floor(min / 60) + ':' + (min % 60);
}
function str_as_min (str) {
var m = str.match(/^(\d\d):(\d\d)/);
return 60 * parseInt(m[1], 10) + parseInt(m[2], 10);
}
您建议的示例输出数组的唯一区别是时间格式(即7:15
而不是07:15
- 您可以轻松修复此问题。
答案 3 :(得分:1)
您需要在跟踪时间的同时循环遍历数组。 jsFiddle
function subtractMinute(time){
var h = +time.substr(0, 2);
var m = +time.substr(3, 2);
if(m > 0){
m -= 1;
}else{
if(h > 0){
h -= 1;
}else{
return false;
}
m = 59;
}
if(h < 10)
h = '0'+h;
if(m < 10)
m = '0'+m;
return h+':'+m+':00';
}
function addMinute(time){
var h = +time.substr(0, 2);
var m = +time.substr(3, 2);
if(m < 59){
m += 1;
}else{
if(h < 22){
h += 1;
}else{
return false;
}
m = 0;
}
if(h < 10)
h = '0'+h;
if(m < 10)
m = '0'+m;
return h+':'+m+':00';
}
//If meetings is already sorted by time
//you can skip this next bit of code
meetings.sort(function(a, b){
return a.start_time > b.start_time? 1: -1;
});
var schedule = [];
var start_time = '00:00:00';
var end_time = '23:59:00';
for(var i=0, l=meetings.length; i<l; i++){
end_time = subtractMinute(meetings[i].start_time);
if(i)
start_time = addMinute(meetings[i-1].end_time);
if((end_time && !i) || (end_time && i && meetings[i-1].end_time < meetings[i].start_time))
schedule.push({meeting: 'free time', start_time: start_time, end_time: end_time});
schedule.push(meetings[i]);
if(i+1 === l){
start_time = addMinute(meetings[i].end_time);
if(start_time)
schedule.push({meeting: 'free time', start_time: start_time, end_time: '23:59:00'});
}
}
console.log(schedule);