Как сравнить (больше чем, меньше чем) время дня с временем встречи? - PullRequest
0 голосов
/ 19 марта 2019

Решено ниже

Я пытаюсь создать чат-бота, который автоматически отменяет назначенную встречу, если она назначена в нерабочее время. Это моя текущая функция:

(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 голосов
/ 20 марта 2019

Хорошо, есть несколько вещей, которые необходимо изменить, например, когда вы setHours(9) и присваиваете результат переменной, для которой вы хотите getHours(), позже, он больше не будет обрабатываться как Date. Посмотрите, как я удалил некоторые вещи из функции (Number (time), Number (..)) и добавил с console.log () несколько примеров того, как вы можете сравнивать даты, просто используя операторы >, <, ==:

function checkCalendarAvailablity (dateTimeStart, dateTimeEnd) {
    var appsoftOpen = new Date();
    var appsoftClose = new Date();
    appsoftOpen.setHours(9);
    appsoftClose.setHours(5);
    var time = dateTimeStart.getHours();
    appsoftClose = appsoftClose.getHours();
    appsoftOpen = appsoftOpen.getHours();

    // Comparison with operators >, <, ==
    console.log(time>appsoftOpen)
    console.log(time<appsoftOpen)
    console.log(time==appsoftOpen)

  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 >= appsoftClose) {
        reject(err || new Error('Requested time falls outside business hours'));
      } else if (err || time <= appsoftOpen) {
        reject(err || new Error('Requested time falls outside business hours'));
      } else {
        resolve(calendarResponse);
      }
    });
  });
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...