gpt4 book ai didi

javascript - 使用 Utilities.formatDate 表示 12/31/2017 一秒 = 1 年

转载 作者:行者123 更新时间:2023-12-01 02:34:00 29 4
gpt4 key购买 nike

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

如果 offset = (-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”

关于javascript - 使用 Utilities.formatDate 表示 12/31/2017 一秒 = 1 年,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086560/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com