gpt4 book ai didi

javascript - 如何根据数组值计算可用时间

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:35:32 26 4
gpt4 key购买 nike

假设我有一个如下所示的数组:

{
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"
}

关于如何在没有插件的情况下完成这项工作有什么建议吗?

最佳答案

This is what I'll do :

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"}]

关于javascript - 如何根据数组值计算可用时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19277348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com