1

I am currently creating a simple sign in sheet that connects to the Google Calendar API. When user enters the prompted information, which includes start Datetime and Endtime, it should create a calendar event based on the user input. I managed to make it so it makes a calendar event. However, the timing are really strange and I don't understand how the times are derived.

For the code below, I would assume that it would create a Calendar event on 2016-12-16 at 2pm, but it doesn't, it creates an event at 2016-12-16 at 9am. Why is that? Thank you!

 function makeEvent(){
  //Get the variable details
  var eventDet = getInput();
  var event = {
      'summary': eventDet[0] + ' Appointment',
      'description': 'Telephone #: ' + eventDet[1] + ' Client Status: ' + eventDet[3],
      'start': {
        'dateTime': '2016-12-16T14:00:00Z',
        'timeZone': 'America/New_York'
      },
      'end': {
        'dateTime': '2016-12-16T14:00:00Z',
        'timeZone': 'America/New_York'
      },
      'attendees': [
        {'email': 'lpage@example.com'},
        {'email': 'sbrin@example.com'}
      ],
      'reminders': {
        'useDefault': false,
        'overrides': [
          {'method': 'email', 'minutes': 24 * 60},
          {'method': 'popup', 'minutes': 10}
        ]
      }
  };
cruise_lab
  • 649
  • 8
  • 21

1 Answers1

0

…Z is always UTC. At 2016-12-16T14:00:00Z, it's 9am in NYC.

To get 2pm in NYC, try either leaving off the Z so that it might get parsed according to the timezone field, or simply pass 2016-12-16T19:00:00Z which is the timestamp you want.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Ok, so how do you derive that. Why is 2pm, 19:00:00z? – cruise_lab Dec 16 '16 at 16:05
  • And when I tried to leave off the z, google api complains that fields are submitted incorrectly – cruise_lab Dec 16 '16 at 16:11
  • You just subtract new yorks timezone offset from the local date. However, really the date library where you get the data from should be able to supply you with that, so don't do it manually. – Bergi Dec 16 '16 at 16:22
  • var startTime_iso = new Date(startTime).toISOString(); So i used this but it is still not giving me the correct time. My input time is datetime-local, the input returns values are like 2016-12-16T14:00:00Z – cruise_lab Dec 16 '16 at 17:57
  • I don't know what `startTime` is (a string?), but you'd need to get it parsed for the local timezone not UTC. Depending on its format, you might want to use a date library for this. – Bergi Dec 16 '16 at 23:33
  • Nvm, I figured it out, removing the z worked thanks a lot :D – cruise_lab Dec 16 '16 at 23:59