简体   繁体   English

Google Apps 脚本 formatDate 使用用户的时区而不是 GMT

[英]Google Apps Script formatDate using user's time zone instead of GMT

In a function within Google Apps Script I am using Utilities.formatDate()在 Google Apps 脚本中的 function 中,我使用的是Utilities.formatDate()

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

The resulting string is a timestamp in the GMT time zone but I need it to be in the current user's time zone, and account for daylight saving time.结果字符串是GMT时区的时间戳,但我需要它位于当前用户的时区,并考虑夏令时。

The documentation links to the Java SimpleDateFormat class documentation but I can't find a list of valid time zones with which I'd replace GMT .该文档链接到Java SimpleDateFormat class 文档,但我找不到可以替换GMT的有效时区列表。

You can directly get the spreadsheet Time Zone like this : 您可以直接获取电子表格时区,如下所示:

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

See doc here 请参阅此处的doc

but Google Apps Script uses these formats if you want to choose it yourself : 但如果您想自己选择,Google Apps脚本会使用这些格式:

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

Google Sheets seems to have a different list of Timezones.....which is causing problems b/c Sheets and AppScript are on different timezones.谷歌表格似乎有不同的时区列表.....这导致问题 b/c 表格和 AppScript 位于不同的时区。 Not sure how to fix or why it is so hard to do anything useful with google and dates....it shouldn't be so hard.不知道如何修复或为什么很难用谷歌和日期做任何有用的事情......它不应该那么难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM