2
 "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.

Ivan
  • 51
  • 1
  • 9
  • See also https://stackoverflow.com/a/49765656/9337071 and https://stackoverflow.com/q/7244246/9337071 for Tasks API datetimes (not quite Calendar, but another Google API) – tehhowch Sep 17 '18 at 19:55

2 Answers2

1

As stated in the Google Calendar resource documentation, the datetime is an RFC3339-formatted string, e.g.

"1985-04-12T23:20:50.52Z"

In fact, it looks like the Google Java client library has a built-in RFC3339 formatter to make this easier. See this answer: How do I parse RFC 3339 datetimes with Java?

Community
  • 1
  • 1
alexwennerberg
  • 121
  • 1
  • 5
  • "Google Java client library" - the question is tagged [tag:javascript] (which has about as much in common with Java as Carpet does with Car). – Quentin Sep 16 '18 at 23:13
  • @Quentin I'm aware, but the body of the original question says Java: "...Date class from Java". May have been a tagging error. – alexwennerberg Sep 16 '18 at 23:16
  • @Quentin Yes I intended to ask about Javascript, but maybe `Date` class from Java is a bad example. `Date` object in Javascript is much easier to format, but `DateTime` does not seem to share any similarity with it. – Ivan Sep 17 '18 at 19:40
0

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);
});
Josh McGee
  • 443
  • 6
  • 16