Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

  1. Should the script's timeZone be set to UTC?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
849 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is a modified version of the answer, without the UI stuff:

function gasTimezoneOffset(date, hour){
  var cal,calTimeZone,calTz,date,dateString,scriptTimeZone,sessionTz;

  var testMonth = "March",
      testDayOfMnth = "26",
      testYr = "2016",
      hour = "10:00",
      timeZoneDiff = 0;

  Logger.log("Script Time Zone: " + Session.getScriptTimeZone());

  dateString = testMonth + " " + testDayOfMnth + ", " + testYr;
  Logger.log("dateString: " + dateString);

  date = new Date(dateString);

  cal = CalendarApp.getDefaultCalendar();
  calTimeZone = cal.getTimeZone();

  calTimeZone = Utilities.formatDate(date, calTimeZone, 'Z');
  scriptTimeZone = Utilities.formatDate(date, Session.getTimeZone(), 'Z');

  calTz = Number(calTimeZone.slice(0,3));
  sessionTz = Number(scriptTimeZone.slice(0,3));

  //If both time zones are the same sign, get the difference between the
  //two.  E.g. -4 and -2.  Difference is 2
  //if each time zone is a different sign, add the absolute values together.
  //-4 and +1 should be 5
  if (calTz < 0 && sessionTz > 0 || calTz > 0 && sessionTz < 0){
    timeZoneDiff = Math.abs(Math.abs(calTz) + Math.abs(sessionTz));
  } else {
    timeZoneDiff = Math.abs(Math.abs(calTz) - Math.abs(sessionTz));
  };

  hour = Number(hour.slice(0,2));    
  return hour + timeZoneDiff;
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...