Dropdown or Date Picker? What’s time got to do with it?

When developing Google Workspace Add-ons for Google Calendar, you may have a need to create a calendar event by converting a string into a date object. The Official Documentation provides an example using the createEvent() method:

// Creates an event for the moon landing and logs the ID.
var event = CalendarApp.getDefaultCalendar().createEvent('Apollo 11 Landing',
    new Date('July 20, 1969 20:00:00 UTC'),
    new Date('July 21, 1969 21:00:00 UTC'));
Logger.log('Event ID: ' + event.getId());

This is easy enough to do. Notice the string inside the new Date object: ‘July 20, 1969 20:00:00 UTC’. This works great for adding known historical events, but what if our add-on is intended to create events on the fly and in the East Coast timezone? The added complexity is dealing with specific timezones and daylight savings time. On the east coast, are we on EST or EDT? To get this right we can run some quick logic in our apps script before creating an event to ensure we’ve got the right time.

function myFunction() {
  const offset = new Date().getTimezoneOffset();
  console.log("Minutes: "+offset);
  if (offset == 240) {
    var tz = "EDT"
  } else {
    var tz = "EST"
  console.log("Timezone: "+tz);

If my add-on is intentionally crafted for the East coast, I can first return the total number of minutes of today’s date from GMT.

const offset = new Date().getTimezoneOffset();

console.log(“Minutes: “+offset);

Four hours or 240 minutes tells me we’re on EDT. If it’s not 240 minutes, then I know it’s EST. This is especially helpful when I’d rather use a dropdown menu to select a time, rather than a date picker.

This way I control the intervals of time available to the end user by displaying a list of times and can pass the value of that selection in the needed format to execute the calendar event creation.