简体   繁体   English

从电子表格中错误的日期提取

[英]Wrong date extraction from Spreadsheet

I want to extract a date from a spreadsheet that is in a "DD dd/mm/yyyy hh:mm:ss" format and put it in another spreadsheet and afterwards create an event in calendar, the problem is that when it is extracted it gets a different time zone than the one from the original spreadsheet, and it changes with different documents. 我想从电子表格中提取日期格式为“ DD dd / mm / yyyy hh:mm:ss”的日期,并将其放入另一个电子表格中,然后在日历中创建一个事件,问题是当提取它时时区与原始电子表格中的时区不同,并且随不同的文档而变化。 The Utilities.formatDate() command doesn't work for me because it appears in the correct time zone. Utilities.formatDate()命令对我不起作用,因为它出现在正确的时区中。

Cell value : mon. 单元格值:星期一。 02/04/2018 12:00:00 02/04/2018 12:00:00

Original Spreadsheet time zone : GMT -800 原始电子表格时区:GMT -800

New Spreadsheet time zone : GMT -800 (also) 新的电子表格时区:GMT -800

function Event(){
  var firstdoc =SpreadsheetApp.openById("ID"); // ouvre le fichier manifest list
  var firstdocsheet = firstdoc.getSheetByName("sheet");
  var date1= new Date(firstdocsheet.getRange(4,10).getValue());
  Logger.log(date1); 
  date1 = Utilities.formatDate(date1, "GMT-800", "dd-MM-yyyy HH:mm:ss");
  Logger.log(date1);   
}

This is what I get: 这是我得到的:

Logger.log([Mon Apr 02 21:00:00 GMT+02:00 2018) Logger.log([2018年4月2日星期一21:00:00 GMT + 02:00 2018)

Logger.log([02-04-2018 11:00:00) Logger.log([02-04-2018 11:00:00)

I know that the code works but I don't get why it takes the wrong date since the beggining, any help please ? 我知道该代码有效,但自开始以来我不明白为什么它使用错误的日期,请帮忙吗?

Verify your code with the example below: 使用以下示例验证您的代码:

function test() {
  var date = new Date('03/08/2018 09:51:00');
  Logger.log(date)
  Logger.log(Utilities.formatDate(date, "GMT-08:00", "dd-MM-yyyy HH:mm:ss"));
}

Log output: 日志输出:

[18-03-08 12:31:31:518 CET] Thu Mar 08 09:51:00 GMT+01:00 2018
[18-03-08 12:31:31:520 CET] 08-03-2018 00:51:00

The mechanism you're using seems to be right, so the problem is either in your cell value or the timezone as pointed out in the comment. 您使用的机制似乎是正确的,因此问题出在您的单元格值或注释中指出的时区。

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

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