如何使用日历的时区来为日期对象设置时区 [英] How to use timeZone of calendar to set timeZone for date object

查看:88
本文介绍了如何使用日历的时区来为日期对象设置时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用日历时区来设置日期对象的时区。我正在寻找适当的技术。我们在全国有几个基地,每个基地都有自己的日历和日常活动日历。我们有多个发布到日历的脚本。我想使用日历的时区来设置日期对象时区,因为用户到处移动到不同的基地,他们的计算机可能没有设置到正确的时区。我们希望避免错误的时间设置。

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. 脚本的时区应该设置为UTC吗?

  1. 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');
}


编辑:
当前有3个日历,它们不是用户日历,只是为每个空中站创建单独的日历。空中电台分别位于不同的时区。当这些工作人员在这些工作站工作时,他们会将日常活动发布到日历中,并且还有几个用户界面脚本,我们将该帖子发布到相同的日历前例如。一个飞行日志。将日历条目发布到任何日历时,时间只与在脚本上设置的时区相关,而不与日历上的时区相关。当创建日期或时间戳记对象时,如何使用日历本身设置的时区。

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.

记录不同时间日期的脚本的最佳做法是什么区?
将脚本时区设置为UTC并执行转换?
您用什么来获取用户的时区,或者在这种情况下,我不在乎用户的时区设置是否过多,我需要使用日历的时区。

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.

推荐答案

好的,这里是解决问题的方法。我可能走了出路或错过了一些简单的东西,但这终于像我希望的那样工作了。随意批评。

Ok here is a solution to the problem. I've probably went way out of my way or missed something simple but this finally works like I was hoping. Feel free to critique. I set the webapp timezone to GMT-0000 just for simplicity.

function uiTest() {
  var app = UiApp.createApplication();

  var startDate = app.createDateBox().setName('startDate').setId('startDate').setWidth('75');
  var startHour = app.createListBox().setName('startHour').setId('startHour').setWidth('45');
  var startMin = app.createListBox().setName('startMin').setId('startMin').setWidth('45');

  var endDate = app.createDateBox().setName('endDate').setId('endDate').setWidth('75');
  var endHour = app.createListBox().setName('endHour').setId('endHour').setWidth('45');
  var endMin = app.createListBox().setName('endMin').setId('endMin').setWidth('45');

  for (h=0;h<24;++h){
  if(h<10){
    var hourstr='0'+h
  }else{
    var hourstr=h.toString()
    }
  startHour.addItem(hourstr)
  endHour.addItem(hourstr)
  }
  for (m=0;m<60;++m){
  if(m<10){
    var minstr='0'+m
  }else{
    var minstr=m.toString()
    }
  startMin.addItem(minstr)
  endMin.addItem(minstr)
  }


  var grid = app.createFlexTable().setId('grid');
  app.add(grid);

  grid.setWidget(0, 0, app.createLabel('Start Date'));
  grid.setWidget(1, 0, startDate);

  grid.setWidget(0, 1, app.createLabel('Hour'));
  grid.setWidget(1, 1, startHour);


  grid.setWidget(0, 2, app.createLabel('Min'));
  grid.setWidget(1, 2, startMin);

  grid.setWidget(2, 0, app.createLabel('End Date'));
  grid.setWidget(3, 0, endDate);

  grid.setWidget(2, 1, app.createLabel('Hour'));
  grid.setWidget(3, 1, endHour);

  grid.setWidget(2, 2, app.createLabel('Min'));
  grid.setWidget(3, 2, endMin);



  app.add(app.createButton('Submit', app.createServerHandler('submitUiTest').addCallbackElement(grid)));

  SpreadsheetApp.getActiveSpreadsheet().show(app);  
}


function submitUiTest(e) {
  var app = UiApp.getActiveApplication();
  var cal = CalendarApp.getCalendarById('');//Info Sys Calendar set to Central Time
  //var cal = CalendarApp.getCalendarById('');//Fort Bliss (Mountain Time)
  var calTimeZone = cal.getTimeZone();

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');


  var startTime = e.parameter.startDate
  startTime.setHours(gasTimezoneOffset(startTime, e.parameter.startHour, calTimeZone), e.parameter.startMin, 0);
  Logger.log('startTime: '+Utilities.formatDate(startTime, calTimeZone, 'M/d/yyyy HH:mm z'));

  var endTime = e.parameter.endDate
  endTime.setHours(gasTimezoneOffset(endTime, e.parameter.endHour, calTimeZone), e.parameter.endMin, 0);
  Logger.log('endTime: '+endTime)

  var timeStamp = Utilities.formatDate(startTime, calTimeZone, 'M/d/yyyy HH:mm z');

  cal.createEvent(timeStamp, new Date(startTime), new Date(endTime));

  ss.appendRow([startTime, endTime]);

  return app;
}


function gasTimezoneOffset(date, hour, calTimeZone){
  var calTz = new Number(Utilities.formatDate(date, calTimeZone, 'Z').substr(1,2));
  var sessionTz = new Number(Utilities.formatDate(date, Session.getTimeZone(), 'Z').substr(1,2));

  switch (Utilities.formatDate(date, calTimeZone, 'Z').substring(0,1)){
    case '+':
      var timeZoneOffset = sessionTz - calTz;
      break;
    case '-':
      var timeZoneOffset = sessionTz + calTz;
      break;
  }

  hour = new Number(hour);

  return hour + timeZoneOffset;
}

这篇关于如何使用日历的时区来为日期对象设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆