使用Utilities.formatDate一秒钟= 12年31月1日的1年

问题描述 投票:0回答:1

使用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”

if offset =( - 1)则new_d =“12/30/2017”

所以1毫秒= 1年?

EET = GMT + 2 =脚本时区

工作表时区= GMT + 3

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

我已经完成了+/- 1小时,+ / - 1小时+/- 1秒的偏移,从12/31/2017开始仍然无法实现12/31/2017。

我这样打补丁

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

但寻找可靠的解决方案。谢谢你提前回答。

javascript date google-apps-script simpledateformat date-formatting
1个回答
2
投票

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

每个SimpleDateFormat规范的格式

对于SimpleDateFormat,你使用Y作为一周,y作为一年。由于12/31/2017周是2018年的第一周,它将显示为2018年。

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

© www.soinside.com 2019 - 2024. All rights reserved.