javascript - Google Apps Script formatDate 使用用户时区而不是 GMT

标签 javascript time google-apps-script timezone timezone-offset

在我使用的 Google Apps 脚本中的函数中 Utilities.formatDate()

let formattedTimestamp = Utilities.formatDate(new Date(), "GMT", "hh:mm a");

结果字符串是 GMT 时区的时间戳,但我需要它处于当前用户的时区,并考虑夏令时。

文档链接到 Java SimpleDateFormat class documentation但我找不到可用于替换 GMT 的有效时区列表。

最佳答案

您可以像这样直接获取电子表格时区:

  var addedDate = sheet.getRange(1,1).getValue();
  var addedTime = Utilities.formatDate(addedDate, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), "hh:mm a");

参见 doc here

但如果您想自己选择,Google Apps 脚本会使用这些格式:

http://joda-time.sourceforge.net/timezones.html

关于javascript - Google Apps Script formatDate 使用用户时区而不是 GMT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18596933/

相关文章:

javascript - google.script.run独立使用

google-apps-script - 如何使用 Utilities.sleep() 函数

javascript - 如何在 Select2 中从数据源设置 data-* ?

javascript - 使用 Live 方法将函数绑定(bind)到动态创建的复选框

javascript - Redux 中的容器组件太多(n 个数字,其中 n 未知),担心性能?

java - 将时间转换为 24 小时格式

python - 算法的时间分析

javascript - 如何覆盖 jQuery promise 回调

mysql - 另一列中下一个最接近的日期时间

google-apps-script - 如何从 protected 单元格中删除编辑器或永久保护 Google 表格中的单元格