这就是我要做的:
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));
控制台日志:
[{"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"}]