Using .Net API wrapper for Google Calendar API.

  1. First get primary calendar id
  2. Get timezone of primary calendar (returns good data, e.g., "America/Los_Angeles")
  3. Create a calendar event. Set start time and end time. Set timezone.
Dim eStart As New EventDateTime
eStart.DateTime = _startAt
eStart.TimeZone = GoogleTimeZone
Dim eEnd As New EventDateTime
eEnd.DateTime = _endAt
entry.Start = eStart
entry.End = eEnd
eEnd.TimeZone = GoogleTimeZone
CalService.Events.Insert(entry, calendarid).Execute()

But the events are being created at 3am when the start time specified is 11am.

Google API documentation states "A time zone offset is required unless a time zone is explicitly specified in timeZone" and for timezone "The time zone in which the time is specified. (Formatted as an IANA Time Zone Database name, e.g. "Europe/Zurich".)".

Timezone value is being specified properly.

Basically, it is not making any difference whether or not timezone is specified. Event is created in GMT in google calendar. What is wrong here?


Fixed it (or let's just say hacked it). Google .Net API wrappers are absolutely crap (and this goes to wrapper of all of their APIs and not just Calendar API).

The issue was that event.Start and event.End automatically converts dates and add a "Z" at the end. This tells Google that the date is in GMT format. There is no reason for putting a "Z" because even without it, Google considers GMT. So basically, event.TimeZone=value was being disregarded because the time was appended by "Z".

After I removed the "Z", everything worked ok.

entry.Start.DateTimeRaw = replace(entry.Start.DateTimeRaw,"Z","")
entry.End.DateTimeRaw = replace(entry.End.DateTimeRaw,"Z","")
  • Its an old question but I think timezone and DST was not an issue after Calendar ID was added to the entry. So first get the default calendar ID or the calendar ID of the user selected calendar and use that calendar id in setting calendar events. – Allen King Mar 31 '16 at 6:05

I fixed it by creating an instance of a DateTime object that uses the DateTimeKind enum as one of the constructors. I found the default DateTime.Kind property value is DateTimeKind.Utc when deserializing a JSON date. That's why a Z (UTC) value is in the Raw for me. The time zone value will be correct when DateTimeKind.Local is applied to the DateTimeKind argument in one of the constructors that takes it.

DateTime dt = new DateTime(oldDateTime.Ticks, DateTimeKind.Local);
DateTime dt = new DateTime(yearVar, monthVar, dayVar, hourVar, minuteVar, secondVar, DateTimeKind.Local);
  • hi. i have the same issue. but using the local kind doesnt seem to make a diference in the calendar UI. the raw does seem to change now, but the browser shows wrong time. thanks! – Yisroel M. Olewski Oct 5 '16 at 9:01

Instead of setting the Datetime property that is part of the Start and End objects, you should give the DateTimeRaw a value and assign a timezone to it like this:

     eventItem.Start = new EventDateTime()
         DateTimeRaw = input.Start.ToString("yyyy-MM-ddTHH:mm:ss"),
         TimeZone = "America/New_York"

Noticed that I'm not adding a Z or any timezone representation at the end of the string format. That should fix the issue and should prevent google from ignoring the timezone property when you've set a value.

In addition, if you hover over the DateTime property of the Start or End object in Visual Studio, it is described as the following: DateTime representation of EventDateTime.DateTimeRaw(see image).

enter image description here

Which in this case, replacing the Z value for an empty string will only make the problem worst because your DateTime property will also be updated. I hope this is helpful to anyone in the future.

  • Only if I knew the timezone of user's google calendar. – Allen King Jul 20 '19 at 0:01
  • I did not see a way to fetch it, which sucks. However, because my logic fetches events right before attempting to insert one, the timezone is part of the metadata for the events object returned. – jorgeAChacon Aug 2 '19 at 20:22

Your Answer

