英文:
datetime switching timezone from google app script to google sheets intentionally
问题
我试图在Google表格单元格中填入当前日期和时间,日期没有问题,但时间偏了6小时。
这是我使用的代码:
function start() {
var ss = SpreadsheetApp.getActive();
var timestamp = new Date();
Logger.log('start, timestamp: ' + timestamp);
var date = Utilities.formatDate(timestamp, 'Europe/Brussels', "dd/MM/yyyy");
Logger.log('date: ' + date);
var time = Utilities.formatDate(timestamp, 'Europe/Brussels', "HH:mm:ss");
Logger.log('time: ' + time);
var sheet = SpreadsheetApp.getActive().getSheetByName(SheetName);
sheet.appendRow([date,time]);
}
日期和时间的日志确实给我预期的日期和时间,但一旦appendRow
完成后,在Google表格中,我得到了偏移的时间...
我尝试过自己调整时间,以纠正到正确的值,但没有改变任何事情。
英文:
I'm trying to fill some google sheets cells with the current date and time, date is ok, but the time is off by 6 hours.
This is the code I used:
function start() {
var ss = SpreadsheetApp.getActive();
var timestamp = new Date();
Logger.log('start, timestamp: ' + timestamp);
var date = Utilities.formatDate(timestamp, 'Europe/Brussels', "dd/MM/yyyy");
Logger.log('date: ' + date);
var time = Utilities.formatDate(timestamp, 'Europe/Brussels', "HH:mm:ss");
Logger.log('time: ' + time);
var sheet = SpreadsheetApp.getActive().getSheetByName(SheetName);
sheet.appendRow([date,time]);
}
The logs of the date and time do give me the expected date and time, but once the appendRow is done, in Google Sheets, I'm getting the offset time...
I have tried to offset it myself, to correct to the right value, but that didn't change anything.
答案1
得分: 2
这有点奇怪,我测试了你的代码,对我来说是有效的。
它正确显示了'Europe/Brussels'的时区。还使用var time = Utilities.formatDate(timestamp, 'GMT+1', "HH:mm:ss");
进行测试,两者都在执行日志
和单元格中显示了正确的时间。
如果你在新的Apps Script中测试代码,它也显示了错误的时区吗?你是否在项目设置
> 常规设置
中检查了时区?
如果经过所有这些步骤后仍然存在相同的问题,我建议按照此链接中的步骤创建一个问题跟踪器:https://developers.google.com/apps-script/support#developer_product_feedback。
英文:
That is odd, I tested your code and it works for me.
It's properly showing the timezone of 'Europe/Brussels'. Also, tested using var time = Utilities.formatDate(timestamp, 'GMT+1', "HH:mm:ss");
and both display the correct time inside the Execution log
and the cell.
If you test the code in a new Apps Script does it show the incorrect time zone too? Have you reviewed the time timezone inside Project Settings
> General Settings
?
If after all that you still have the same issue, I will recommend creating an issue tracker by following the steps here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论