我有一个 Google Apps 脚本,它从电子表格上的给定行中提取时间,然后转录它。然而,时区在此过程中变得完全困惑。
相关代码(功能位被剪掉,因为我认为它们不相关):
var timezone = "GMT+" + new Date().getTimezoneOffset()/60;
function dysfunction(row) {
this.time = new Date (sheet.getRange(row,6).getValue());
this.time.setFullYear(2020);
this.timeString = Utilities.formatDate(this.time, timezone, "hh:mm a");
Logger.log(timezone);
Logger.log(this.time);
Logger.log(this.timeString);
}
Stackdriver 登录执行:
May 4, 2020, 9:43:52 PM Info GMT+6
May 4, 2020, 9:43:52 PM Info Wed Dec 30 14:00:00 GMT-07:00 2020
May 4, 2020, 9:43:52 PM Info 03:00 AM
我的正确时区实际上是 GMT-07:00,但由于某种原因,脚本将我的时区检测为 GMT+6。我已在项目属性中设置了正确的时区,甚至在 list 中将时区声明为“美国/埃德蒙顿”。
下一步我该去哪里找出为什么这个东西对正确读取时区不感兴趣?
最佳答案
我想提出以下修改。
来自:
Utilities.formatDate(this.time, timezone, "hh:mm a")
致:
Utilities.formatDate(this.time, SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "hh:mm a")
引用文献:
关于javascript - Google Apps 脚本中的时区不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61605483/