0

I have created a dialogflow chatbot it collects date and time from user then I used google calendar api to use that date and create an event in googe calendar I now need to create another event the day before the users input

this is the code i use to set the event

 const location = agent.parameters.address;
 const appointment_name = agent.parameters.name;
 var date1 = agent.parameters.date;
 var date2 = (date1()- 1);
 const dateTimeStart2 = new Date(Date.parse(date2.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('+')[0] + timeZoneOffset));
 const dateTimeStart = new Date(Date.parse(agent.parameters.date.split('T')[0] + 'T' + agent.parameters.time.split('T')[1].split('-')[0] + timeZoneOffset));
 const dateTimeEnd2 = new Date(new Date(dateTimeStart2).setHours(dateTimeStart2.getHours() + 1));
 const appointmentTimeString = dateTimeStart.toLocaleString(
   'en-US',
   { month: 'long', day: 'numeric', hour: 'numeric', timeZone: timeZone }
 );

function createCalendarEvent (dateTimeStart, dateTimeEnd, appointment_name, location) {
 return new Promise((resolve, reject) => {
   calendar.events.list({
     auth: serviceAccountAuth,
     calendarId: calendarId,
     timeMin: dateTimeStart.toISOString(),
     timeMax: dateTimeEnd.toISOString()
   }, (err, calendarResponse) => {

     if (err || calendarResponse.data.items.length > 0) {
       reject(err || new Error('Requested time conflicts with another appointment'));
     } else {

       calendar.events.insert({ auth: serviceAccountAuth,
         calendarId: calendarId,
         resource: {
  'summary': appointment_name ,
  'description': location,
  'start': {
    'dateTime': dateTimeStart,
  },
  'end': {
    'dateTime': dateTimeEnd,
  },
} ,
       }, (err, event) => {
         err ? reject(err) : resolve(event);
       }
       );
     }
   });
 });
bahaa
  • 67
  • 1
  • 11

1 Answers1

0

The most direct way would be to create new Date objects by subtracting 1 day from the dateTimeStart and dateTimeEnd objects, and then using these objects in a similar call to the calendar API.

Prisoner
  • 49,922
  • 7
  • 53
  • 105
  • Yeah but how to subtract that 1 day can you help me with the code sample? – bahaa Jun 16 '19 at 18:42
  • If something doesn't work, update your question to show what you tried, what results you got, and what error, if any, you got. Just telling us "it didn't work" doesn't give us much to help you, particularly if it did work for us. We want to help you, but you need to give us info to help you. – Prisoner Jun 19 '19 at 21:30