What is datetime object in Google API, and how to format its string output?

Viewed 3985
 "start": {
"date": date,
"dateTime": datetime,
"timeZone": string
}...

This is from the resource representations of Google Calendar API Events class(?). The dateTime field is supposedly a datetime object. I could not find any resources regarding this class or any of its functions, nor did I find a way to easily format its string output like for Date object.

Thank you!

[EDIT]From How to convert date in RFC 3339 to the javascript date object(milliseconds since 1970) it looks like DateTime RFC 3339 string can be parsed into a Date object, which should solve the problem.

2 Answers

it looks like you've already got this sorted out but I just want to share my solution here in case someone else finds this useful.

Adding dateTime to the Google Calendar API seems to be most easily achieved by manipulating a Date object until it represents the appropriate date/time and only then calling the .toISOString() method on it to convert it into the format required by the Google Calendar API

also for the reference here is a list of timezones I found helpful

let startDateTime = new Date();

let finishDateTime = new Date();
finishDateTime.setTime(finishDateTime.getTime() + (1*60*60*1000));

const event = {
    'summary': 'test event',
    'start': {
        'dateTime': startDateTime.toISOString(),
        'timeZone': 'Australia/Melbourne'
    },
    'end': {
        'dateTime': finishDateTime.toISOString(),
        'timeZone': 'Australia/Melbourne'
    }
};

const request = gapi.client.calendar.events.insert({
    'calendarId': 'primary',
    'resource': event
});

request.execute(function(event) {
    console.log('Event created: ' + event.htmlLink);
});
Related