Luke Grech
Luke Grech

Reputation: 185

Google API start and end time format issue Node.JS

I am having problems using a variable that contains data for a Google Calendar Event creation in Node.js. I tried many different time formats for this but I received strange errors. I tried declaring a variable identical to one that works but I still got errors.

//These variables work
        const eventStartTime = new Date();
        eventStartTime.setDate(eventStartTime.getDay() + 2);
    
        const eventEndTime = new Date();
        eventEndTime.setDate(eventEndTime.getDay() + 2);
        eventEndTime.setMinutes(eventEndTime.getMinutes() + 45);
    
        //These variables give a 'Bad Request Error', although they are identical to the previous 2 variables when console.logged
        let eventStart = calendarLesson.date + " " + calendarLesson.startTime + ":00 GMT+0100 (Central European Standard Time)";
        let eventEnd = calendarLesson.date + " " + calendarLesson.endTime + ":00 GMT+0100 (Central European Standard Time)";
        
        //I also tried setting a start and end time variable eg. let testTime= "2020-07-12T09:30:00.0z", which did not give any issues in the query execution
        const event = {
            summary: 'Lesson',
            location: "Classroom",
            description: "This is a lesson",
            start: {
                dateTime: eventStart,
                timeZone: 'CET'
            },
            end: {
                dateTime: eventEnd,
                timeZone: 'CET'
            },
            attendee:[{
                'email': calendarEmailAddress
            }],
            reminders: {
                //reminder
            },
        }

1st set of variables console.log: Thu Jan 21 2021 12:00:00 GMT+0100 (Central European Standard Time) variable console.log: Thu Jan 21 2021 12:00:00 GMT+0100 (Central European Standard Time)

2nd set of variables I also tried setting a start and end time variable eg. "2020-07-12T09:30:00.0z", which did not give any issues in the query execution, but is not ideal as I need to use the variables to set the time. I would really appreciate your help as I have been stuck on this small but annoying bug for hours.

Upvotes: 0

Views: 374

Answers (1)

Kristkun
Kristkun

Reputation: 5953

Based on Events.insert(), here are the accepted date and datetime formats:

Date:

The date, in the format "yyyy-mm-dd"

Datetime:

The time, as a combined date-time value (formatted according to RFC3339). A time zone offset is required unless a time zone is explicitly specified in timeZone.


Example Internet date/time format:

1985-04-12T23:20:50.52Z

1996-12-19T16:39:57-08:00


Please remove " (Central European Standard Time)" in your date-time string. I tried running your code snippet on an online javascript compiler and I received a different output.

enter image description here

Upvotes: 1

Related Questions