如果在nodejs中给出了繁忙的时段,如何找到一天的空闲时段
how to find free time slot of a day if busy slots are given in nodejs
例如具体日期的诊所工作时间如下:
var workingStartTime = 2018-05-12T08:00:00.000Z;
var workingEndTime = 2018-05-12T22:00:00.000Z;
同日已预约的医生如下:
var busySlots =
[ {
start_time: '2018-05-12T14:30:00.000Z',end_time:'2018-05-12T17:45:00.000Z'
},
{
start_time: '2018-05-12T20:30:00.000Z',end_time: '2018-05-12T21:45:00.000Z'
}]
我需要获取从 2018-05-12T08:00:00.000Z
到 2018-05-12T22:00:00.000Z
之间大于 60 分钟的可用医生时间段
在 nodejs 中使用最少代码行的一天时间。
以下问题与我的查询类似:
How to find free time slot based on available time slot and booked time slot?
但是上面的代码在 php 中,我需要用 nodejs 编写的代码。
下面的代码可能没有优化,但对我有用:
function (cb){
var duration = 60;
var freeSlots = [];
var workingStartTime = 2018-05-12T08:00:00.000Z;
var workingEndTime = 2018-05-12T22:00:00.000Z;
var busySlots = [];
busySlots.push({ start_time: "2018-05-12T14:30:00.000Z",end_time: "2018-05-12T17:45:00.000Z" }) ;
busySlots.push({ start_time: "2018-05-12T20:30:00.000Z",end_time: "2018-05-12T21:45:00.000Z" }) ;
var beginAt = workingStartTime;
var overAt = workingEndTime;
var count = 0;
var last = busySlots.length;
async.forEach(busySlots, function (item, callback){
/*** Library funcition to gind difference between two dates (Minutes) ***/
var diff = libFunc.getRange(beginAt, item.start_time,TIME_UNITS.MINUTES);
if(diff > duration){
let free = {"start_time":beginAt , "end_time":item.start_time};
freeSlots.push(free);
beginAt = item.end_time;
count += 1;
/** Process for end slot **/
if(last == count){
var diff = libFunc.getRange(item.end_time, overAt, TIME_UNITS.MINUTES);
if(diff > duration){
let free = {"start_time":item.end_time , "end_time":overAt};
freeSlots.push(free);
callback();
}else{
callback();
}
}else{
callback();
}
/** Process for end slot **/
}else{
beginAt = item.end_time;
count += 1;
/** Process for end slot **/
if(last == count){
var diff = libFunc.getRange(item.end_time, overAt, TIME_UNITS.MINUTES);
if(diff > duration){
let free = {"start_time":item.end_time , "end_time":overAt};
freeSlots.push(free);
callback();
}else{
callback();
}
}else{
callback();
}
/** Process for end slot **/
}
}, function(err) {
// console.log(freeSlots);
cb(null);
});
},
例如具体日期的诊所工作时间如下:
var workingStartTime = 2018-05-12T08:00:00.000Z;
var workingEndTime = 2018-05-12T22:00:00.000Z;
同日已预约的医生如下:
var busySlots =
[ {
start_time: '2018-05-12T14:30:00.000Z',end_time:'2018-05-12T17:45:00.000Z'
},
{
start_time: '2018-05-12T20:30:00.000Z',end_time: '2018-05-12T21:45:00.000Z'
}]
我需要获取从 2018-05-12T08:00:00.000Z
到 2018-05-12T22:00:00.000Z
之间大于 60 分钟的可用医生时间段
在 nodejs 中使用最少代码行的一天时间。
以下问题与我的查询类似:
How to find free time slot based on available time slot and booked time slot?
但是上面的代码在 php 中,我需要用 nodejs 编写的代码。
下面的代码可能没有优化,但对我有用:
function (cb){
var duration = 60;
var freeSlots = [];
var workingStartTime = 2018-05-12T08:00:00.000Z;
var workingEndTime = 2018-05-12T22:00:00.000Z;
var busySlots = [];
busySlots.push({ start_time: "2018-05-12T14:30:00.000Z",end_time: "2018-05-12T17:45:00.000Z" }) ;
busySlots.push({ start_time: "2018-05-12T20:30:00.000Z",end_time: "2018-05-12T21:45:00.000Z" }) ;
var beginAt = workingStartTime;
var overAt = workingEndTime;
var count = 0;
var last = busySlots.length;
async.forEach(busySlots, function (item, callback){
/*** Library funcition to gind difference between two dates (Minutes) ***/
var diff = libFunc.getRange(beginAt, item.start_time,TIME_UNITS.MINUTES);
if(diff > duration){
let free = {"start_time":beginAt , "end_time":item.start_time};
freeSlots.push(free);
beginAt = item.end_time;
count += 1;
/** Process for end slot **/
if(last == count){
var diff = libFunc.getRange(item.end_time, overAt, TIME_UNITS.MINUTES);
if(diff > duration){
let free = {"start_time":item.end_time , "end_time":overAt};
freeSlots.push(free);
callback();
}else{
callback();
}
}else{
callback();
}
/** Process for end slot **/
}else{
beginAt = item.end_time;
count += 1;
/** Process for end slot **/
if(last == count){
var diff = libFunc.getRange(item.end_time, overAt, TIME_UNITS.MINUTES);
if(diff > duration){
let free = {"start_time":item.end_time , "end_time":overAt};
freeSlots.push(free);
callback();
}else{
callback();
}
}else{
callback();
}
/** Process for end slot **/
}
}, function(err) {
// console.log(freeSlots);
cb(null);
});
},