I want to use the calendar timezone to set the time zone of a date object. I'm looking for the proper technique. We have several bases around the nation, and each has their own calendar for journal and daily activities. We have multiple scripts that post to the calendars. I want to use the timezone of the calendar to set the date Object timezone, because the users travel around to different bases, and their computers might not be set to the correct time zone. We want to avoid incorrect time settings.
Should the script's timeZone be set to UTC?
This is where I'm currently at:
function submitUiTest(e) { var app = UiApp.getActiveApplication(); var cal = CalendarApp.getCalendarById('calendarId'); var timeZone = cal.getTimeZone(); var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheet = ss.getSheetByName('Sheet1'); var startTime = e.parameter.startDate startTime.setHours(e.parameter.startHour, e.parameter.startMin, 0) startTime = formatTime(startTime, timeZone); Logger.log(startTime) var endTime = e.parameter.endDate endTime.setHours(e.parameter.endHour, e.parameter.endMin, 0); endTime = formatTime(endTime, timeZone); Logger.log(endTime) cal.createEvent('TimeZone Test', new Date(startTime), new Date(endTime)); ss.appendRow([startTime, endTime]); return app; } function formatTime(time, timeZone){ return Utilities.formatDate(time, (timeZone-time.getTimezoneOffset()), 'M/d/yyyy HH:mm'); }
Edit: Currently there are 3 calendars, they are not user calendars, just each a separate calendar created for individual Air Stations. The air stations are each in separate time zone's. As crew members work at these stations they post daily activities to the calendars, and there are also several Ui scripts we have that post to the same calendars ex. a flight log. When an entry to a calendar is posted to any calendar, the time relates only to the timezone set on the script, not the timezone on the calendar. When the date or timestamp object is created, how can I use the timeZone that the calendar itself is set to.
What is best practice for scripts that record dates for different time zones? Set the script timezone to UTC and do the conversion? What do you use to get the user's timezone or in this case, I don't care what the user's timezone is set too, I need to use the timezone of the calendar.