我有一个日期存储如下:
existingDate = Wed Apr 30 2014 00:00:00 GMT+0100 (BST)当我使用Utilities.formatDate格式化日期时,日期将更改为前一天。
var formattedDate = Utilities.formatDate(new Date(existingDate), "GMT", "dd/MM/yyyy");然后将格式化日期设置为29/04/2014和,而不是 30/04/2014。
有没有其他人见过这种行为。
发布于 2014-04-04 11:39:50
Utilities.formatDate似乎工作得很好。
您有4月30日午夜在格林尼治标准时间-1,但你告诉它格式化这个日期在一个不同的时区GMT,或更明确的GMT-0。预期的结果确实是4月29日23h。
Utilities.formatDate中的第二个参数必须是您想要的时区。
发布于 2017-01-27 06:55:17
这个google脚本还有其他一些问题。如果我想要AEST时区,但只有1/27/2017的日期,那么Utilities.formatDate("1/27/2017“、"AEST”、“yyyy dd”)将返回一个值为2017-01-26,这比它应该的日期早一天!但是,如果我有时间使用它,比如"1/27/2017 12:00:00“,那么它返回正确的一天。但是,如果我使用"GMT+11“作为时区,即使没有时间结束,如果返回正确的一天。
例子-
Logger.log('Example 1='+Utilities.formatDate(new Date("1/27/2017"),"AEST","yyyy-MM-dd")); Logger.log('Example 2='+Utilities.formatDate(new Date("1/27/2017 12:00:00"),"AEST","yyyy-MM-dd")); Logger.log('Example 3='+Utilities.formatDate(new Date("1/27/2017"),"GMT+11","yyyy-MM-dd"));
产出-
例1=2017-01-26
例2=2017-01-27
案例3=2017-01-27
从没有时间的电子表格单元格中读取日期的解决方法是添加时间,并使用诸如"AEST“或类似的时区,如下所示-
var displayDate = sheet.getRange(a1Notation).getDisplayValue(); // Not .getValue();
Logger.log(Utilities.formatDate(new Date(displayDate + " 12:00:00"), "AEST", "yyyy-MM-dd"));但是,"GMT+11“会更容易一些,因为您不需要添加时间。
https://stackoverflow.com/questions/22860477
复制相似问题