Javascript 哪些日期格式是符合 IETF 的 RFC 2822 时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14914739/
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
Which date formats are IETF-compliant RFC 2822 timestamps?
提问by Oriol
I need to parse dates in JavaScript. The format is
我需要在 JavaScript 中解析日期。格式是
[2 digits day]/[2 digits month]/[4 digits year] [2 digits hour (24 mode)]:[2 digits minute]
[2 位数字日]/[2 位数字月份]/[4 位数字年份] [2 位数字小时(24 模式)]:[2 位数字分钟]
For example, 16/02/2013 21:00
例如, 16/02/2013 21:00
But if I do new Date('16/02/2013 21:00').toString(), it gives 'Wed Apr 02 2014 21:00:00 GMT+0200 (Hora de verano romance)'.
但如果我这样做new Date('16/02/2013 21:00').toString(),它会给'Wed Apr 02 2014 21:00:00 GMT+0200 (Hora de verano romance)'.
I guess that's because my dates don't follow IETF RFC 2822 Date and Time Specification. Then, I should convert my string, and I want to convert it to the most similar compliant format (because it should be easier to convert). But http://tools.ietf.org/html/rfc2822#page-14is hard to understand, so I don't know which is the most similar format.
我想这是因为我的日期不遵循 IETF RFC 2822 日期和时间规范。然后,我应该转换我的字符串,我想将它转换为最相似的兼容格式(因为它应该更容易转换)。但是http://tools.ietf.org/html/rfc2822#page-14很难理解,所以不知道哪个格式最相似。
Is there a list with examples of the allowed formats?
是否有包含允许格式示例的列表?
回答by Paul Sweatte
MSDNhas several examples of valid date formats:
MSDN有几个有效日期格式的例子:
document.writeln((new Date("2010")).toUTCString());
document.writeln((new Date("2010-06")).toUTCString());
document.writeln((new Date("2010-06-09")).toUTCString());
// Specifies Z, which indicates UTC time.
document.writeln((new Date("2010-06-09T15:20:00Z")).toUTCString());
// Specifies -07:00 offset, which is equivalent to Pacific Daylight time.
document.writeln((new Date("2010-06-09T15:20:00-07:00")).toGMTString());
// Specifies a non-ISO Long date.
document.writeln((new Date("June 9, 2010")).toUTCString());
// Specifies a non-ISO Long date.
document.writeln((new Date("2010 June 9")).toUTCString());
// Specifies a non-ISO Short date and time.
document.writeln((new Date("6/9/2010 3:20 pm")).toUTCString());
// Output:
// Fri, 1 Jan 2010 00:00:00 UTC
// Tue, 1 Jun 2010 00:00:00 UTC
// Wed, 9 Jun 2010 00:00:00 UTC
// Wed, 9 Jun 2010 15:20:00 UTC
// Wed, 9 Jun 2010 22:20:00 UTC
// Wed, 9 Jun 2010 07:00:00 UTC
// Wed, 9 Jun 2010 07:00:00 UTC
// Wed, 9 Jun 2010 22:20:00 UTC
Gotchas
陷阱
There's a matrix of cross-browser inconsistenciesas well.
还有一个跨浏览器不一致的矩阵。
References
参考
回答by RobG
This question seems to be asking "What formats are required to be parsed by ECMSCript implementations".
这个问题似乎在问“ ECMSCript 实现需要解析哪些格式”。
Prior to ECMAScript Ed 5 (2011), parsing was entirely implementation dependent. The formats that ECMAScript implementations are required to parse can be summarised as:
在 ECMAScript Ed 5 (2011) 之前,解析完全依赖于实现。ECMAScript 实现需要解析的格式可以概括为:
- A single (though slightly modified) version of ISO 8601 extended format called the "date time string format" introduced in ECMAScript ed 5 (2011). It differs from ISO 8601 in that date-only forms are treated as UTC, not local and the timezone offset must have a colon between the hours and minutes.
- The format produced by an implementation's own toStringmethod, which was standardised in ECMAScript 2018
- The format produced by an implementation's own toUTCStringmethod, also standardised in ECMAScript 2018
- 在 ECMAScript ed 5 (2011) 中引入的称为“日期时间字符串格式”的 ISO 8601 扩展格式的单个(尽管略有修改)版本。它与 ISO 8601 的不同之处在于,仅将日期形式视为 UTC,而不是本地,并且时区偏移量必须在小时和分钟之间有一个冒号。
- 由实现自己的toString方法生成的格式,在 ECMAScript 2018 中标准化
- 由实现自己的toUTCString方法生成的格式,也在 ECMAScript 2018 中标准化
Parsing of any other format remains implementation dependent and there are differences, so the general rule is "don't use the built-in parser".
任何其他格式的解析仍然依赖于实现并且存在差异,因此一般规则是“不要使用内置解析器”。

