简体   繁体   English

使用Utilities.formatDate,2017年12月31日为1秒= 1年

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

Have curious results converting date 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?!

I've tried to develop a patch and get unexpected results for this function 我试图开发一个补丁并获得此功能的意外结果

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")
}

if offset = 1 then new_d = "12/31/2018" 如果offset = 1,则new_d =“ 12/31/2018”

if offset = (-1) then new_d = "12/30/2017" 如果offset =(-1),则new_d =“ 12/30/2017”

so 1 milisecond = 1 year? 所以1毫秒= 1年?

EET = GMT+2 = script timezone EET = GMT + 2 =脚本时区

worksheet timezone = GMT+3 工作表时区= GMT + 3

from debugger i've noticed that date is spoiled by Utilities.formatDate() 从调试器中,我注意到Utilities.formatDate()破坏了日期

I've triede +/-1h, +/-1h+/-1sec offsets and still can't get just 12/31/2017 from 12/31/2017. 我尝试了+/- 1h,+ /-1h +/- 1sec偏移量,但仍然无法从2017年12月31日仅获得2017年12月31日。

I've patched it like this 我已经这样修补了

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

but look for solid solution. 但寻找可靠的解决方案。 thank you for answer in adwance. 谢谢您的提前答复。

The third parameter of formatDate(date, timeZone, format) is formatDate(date, timeZone, format)的第三个参数是

a format per the SimpleDateFormat specification 根据SimpleDateFormat规范的格式

For the SimpleDateFormat you use Y for the week year and y for the year. 对于SimpleDateFormat,您将Y用作星期年份,将y用作年份。 Since the week of 12/31/2017 is the first week of 2018, it will show as 2018. 由于2017年12/31/2017日这一周是2018年的第一周,因此它将显示为2018年。

Instead of using "MM/dd/YYYY" , use "MM/dd/yyyy" . 代替使用"MM/dd/YYYY" ,使用"MM/dd/yyyy"

暂无
暂无

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

相关问题 Utilities.formatDate和时区不起作用? - Utilities.formatDate and timezone not working? 使用 new Date() 和 Utilities.formatDate 进行日期操作会抛出意外结果 - Date Manipulation using new Date() & Utilities.formatDate throw Unexpected Results Utilities.formatDate 在第 7 天而不是第 1 天开始一​​周 - Utilities.formatDate starts the week on day 7 instead of day 1 谷歌脚本“Utilities.formatDate”对时区参数不做任何事情 - Google Scripts "Utilities.formatDate" does nothing with the time zone parameter 我怎样才能从 Utilities.formatDate(new Date(), “EST”, “HH”); 到 integer? - How can I cast the return from Utilities.formatDate(new Date(), “EST”, “HH”); to an integer? 一次使用具有相关类div12 div31的jquery选择dom解析器 - selecting on dom parser using jquery with relevant class div12 div31 all at one time 如何将“0:2017-12-31T19:00:00.000Z 1:2018-01-09T19:00:00.000Z”转换为数组? - How to convert "0:2017-12-31T19:00:00.000Z 1:2018-01-09T19:00:00.000Z " to array? 如何使用 moment js 验证 2020-12-31T13:00:00.000Z? - How to validate 2020-12-31T13:00:00.000Z using moment js? 使用两个嵌套循环,在 window 中显示以下行:11 12 13 21 22 23 31 32 33 JavaScript - Using two nested loops, display in the window the following line: 11 12 13 21 22 23 31 32 33 JavaScript Globalize / Cldr : Globalize.formatDate(new Date(), {datetime:"long"}) 导致类似“16. januar 2017. 13.30.17 GMT+1” - Globalize / Cldr : Globalize.formatDate(new Date(), {datetime:"long"}) resulted in something like "16. januar 2017. 13.30.17 GMT+1"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM