如何将一天中的时间(大于,小于)与约会时间进行比较?

时间:2019-03-19 20:52:11

标签: node.js dialogflow

解决以下问题

我正在尝试创建一个聊天机器人,该聊天机器人将在工作时间以外进行约会时自动取消该约会。这是我当前的功能:

(updated)
// This function checks for the availability of the time slot, which starts at 'dateTimeStart' and ends at 'dateTimeEnd'.
// 'dateTimeStart' and 'dateTimeEnd' are instances of a Date object.
function checkCalendarAvailablity (dateTimeStart, dateTimeEnd) {
    var appsoftOpen = new Date();
    var appsoftClose = new Date();
    appsoftOpen.setHours(9);
    ppsoftClose.setHours(5);
    var time = dateTimeStart.getHours();
    var appsoftClose1 = appsoftClose.getHours();
    var appsoftOpen1 = appsoftOpen.getHours();
    time = Number(time);
    appsoftClose = Number(appsoftClose1);
    appsoftOpen = Number(appsoftOpen1);
  return new Promise((resolve, reject) => {
    calendar.events.list({
      auth: serviceAccountAuth, // List events for time period
      calendarId: calendarId,
      timeMin: dateTimeStart.toISOString(),
      timeMax: dateTimeEnd.toISOString()
    }, (err, calendarResponse) => {
      // Check if there is an event already on the Calendar
      if (err || calendarResponse.data.items.length > 0) {
        reject(err || new Error('Requested time conflicts with another appointment'));
      } else if (err || time >= appsoftClose1) {
        reject(err || new Error('Requested time falls outside business hours'));
      } else if (err || time <= appsoftOpen1) {
        reject(err || new Error('Requested time falls outside business hours'));
          }else {
        resolve(calendarResponse);
      }
    });
  });
}

现在,它停止约会的重叠,但仍允许随时进行约会。有什么建议吗?谢谢!

已解决

// This function checks for the availability of the time slot, which starts at 'dateTimeStart' and ends at 'dateTimeEnd'.
// 'dateTimeStart' and 'dateTimeEnd' are instances of a Date object.
function checkCalendarAvailablity (dateTimeStart, dateTimeEnd) {
    var time = dateTimeStart.getHours();
    // adjust for timezone
    time = time - 4;
    console.log(time);
  return new Promise((resolve, reject) => {
    calendar.events.list({
      auth: serviceAccountAuth, // List events for time period
      calendarId: calendarId,
      timeMin: dateTimeStart.toISOString(),
      timeMax: dateTimeEnd.toISOString()
    }, (err, calendarResponse) => {
      // Check if there is an event already on the Calendar
      if (err || calendarResponse.data.items.length > 0) {
        reject(err || new Error('Requested time conflicts with another appointment'));
      } else if (err || time >= 17) {
        reject(err || new Error('Requested time falls outside business hours'));
      } else if (err || time <= 9) {
        reject(err || new Error('Requested time falls outside business hours'));
          }else {
        resolve(calendarResponse);
      }
    });
  });
}

1 个答案:

答案 0 :(得分:0)

好吧,有些事情需要更改,例如当您setHours(9)并将结果分配给以后要getHours()的变量时,它将不再像{ {1}}。看看我如何删除函数中的某些内容(Number(time),Number(..)),并在console.log()中添加一些示例,这些示例说明了如何仅使用运算符Date来比较日期。 ,><

==