一秒 = 1 年 12/31/2017 使用 Utilities.formatDate

one second = 1 year for 12/31/2017 using Utilities.formatDate

使用 Utilities.formatDate()

转换日期有奇怪的结果
function test_date_conversion_1() {
    var in_d_str = "12/31/2017"
    var new_d = Utilities.formatDate(new Date(in_d_str), "EET", "MM/dd/YYYY")
    return new_d
} 
// new_d = "12/31/2018"  2018?!

我已经尝试开发了一个补丁并得到了这个功能的意想不到的结果

function test_date_conversion(offset) {
    //offset = 1
    var in_d_str = "12/31/2017"
    var in_d = new Date(in_d_str)
    var in_d_epoch = in_d.getTime()
    var in_d_epoch_dep_1 = in_d_epoch+offset
    var in_d_d_dep_1 = new Date(in_d_epoch_dep_1)
    var new_d = Utilities.formatDate(in_d_d_dep_1, "EET", "MM/dd/YYYY")
}

如果 offset = 1 那么 new_d = "12/31/2018"

如果偏移量 = (-1) 那么 new_d = "12/30/2017"

所以 1 毫秒 = 1 年?

EET = GMT+2 = 脚本时区

工作表时区 = GMT+3

从调试器我注意到日期被 Utilities.formatDate() 破坏了

我尝试了 +/-1h、+/-1h+/-1sec 偏移量,但仍然无法从 12/31/2017 获得 12/31/2017。

我已经这样打补丁了

if (new_d == "12/31/2018")
    new_d = "12/31/2017";

但要寻找固溶体。 提前谢谢你的回答。

formatDate(date, timeZone, format)的第三个参数是

a format per the SimpleDateFormat specification

对于 SimpleDateFormat,您使用 Y 表示周年,y 表示年。由于12/31/2017的那一周是2018年的第一周,所以会显示为2018年

不使用 "MM/dd/YYYY",而是使用 "MM/dd/yyyy"