問題描述
我有一行在單元格中設置當前日期和時間:
I have a line that sets the current date and time in a cell:
sheet.getRange(1,1).setValue(new Date());
然后我有一行可以根據該單元格的值創建格式化日期:
Then I have a line that creates a formatted date based on that cell's value:
var addedDate = sheet.getRange(1,1).getValue();
var addedTime = Utilities.formatDate(addedDate, "GMT", "hh:mm a");
生成的 addedTime 似乎在 GMT 時區,我需要它在用戶的時區.它通常是美國東部時間 (-0500),但如果我只是從時間中減去 5 小時,那么這并沒有考慮夏令時 (-0400).
The resulting addedTime seems to be in the GMT time zone and I need it to be in the user's time zone. It will usually be US Eastern Time (-0500), but if I simply subtract 5 hours from the time then that doesn't account for daylight saving time (-0400).
我在這里查看了 formatDate 的文檔:https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
I checked the documentation for formatDate here: https://developers.google.com/apps-script/reference/utilities/utilities#formatDate(Date,String,String)
此處鏈接到官方 Java SimpleDateFormat 類文檔:http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
which links to the official Java SimpleDateFormat class documentation here: http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html
...但我無法找到有效時區列表來替換 GMT.
... but I can't find a list of valid time zones there to replace the GMT with.
推薦答案
可以這樣直接獲取電子表格時區:
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");
在此處查看文檔
但如果您想自己選擇,Google Apps 腳本會使用這些格式:
but Google Apps Script uses these formats if you want to choose it yourself :
http://joda-time.sourceforge.net/timezones.html
這篇關于Google Apps 腳本 formatDate 使用用戶的時區而不是 GMT的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!