[英]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.