Javascript UTC 日期字符串是否需要格式说明符“Z”,即使该字符串包含时间偏移量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14714548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:43:44  来源:igfitidea点击:

Does UTC date string require the format specifier "Z" even if that string includes time offset?

javascriptparsingdateutc

提问by Cute_Ninja

For instance,

例如,

Date d = "2012-08-20T15:00:00-07:00";

dhere is a UTCtime with time offset = 07:00. Does it still require Zlike this 2012-08-20T15:00:00-07:00Z? Is this correct?

d这是一个UTC时间,时间偏移 = 07:00。还需要Z这样2012-08-20T15:00:00-07:00Z吗?这样对吗?

If I take this string with Zand pase it using Date.parse()method in JavaScript, it throws an error. Not sure what is wrong!

如果我使用此字符串Z并使用Date.parse()JavaScript 中的方法传递它,则会引发错误。不知道出了什么问题!

回答by Laurence Gonsalves

No, you should not include the "Z" with a time zone offset.

不,您不应包含带有时区偏移量的“Z”。

From rfc3339:

来自rfc3339

  Z           A suffix which, when applied to a time, denotes a UTC
              offset of 00:00; often spoken "Zulu" from the ICAO
              phonetic alphabet representation of the letter "Z".

The "Z" is a zero time offset, so including it with an explicit offset (especially a non-zero one) doesn't makes sense.

“Z”是零时间偏移,因此将其与显式偏移(尤其是非零偏移)包括在内是没有意义的。

回答by Tomasz Nurkiewicz

Quoting W3C note on Date and Time Formats:

引用W3C 关于日期和时间格式的说明

YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)

where:

[...]

TZD  = time zone designator (Z or +hh:mm or -hh:mm)

YYYY-MM-DDThh:mm:ss.sTZD(例如 1997-07-16T19:20:30.45+01:00)

在哪里:

[...]

TZD  = time zone designator (Z or +hh:mm or -hh:mm)

Notice the orword above. You eitherspecify time zone offset orZfor Zulu(no offset).

注意上面的字。你要么指定时区偏移量Z祖鲁语(无偏移)。