[英]Which date formats are IETF-compliant RFC 2822 timestamps?
我需要在 JavaScript 中解析日期。 格式是
[2 位数字日]/[2 位数字月份]/[4 位数字年份] [2 位数字小时(24 模式)]:[2 位数字分钟]
例如,16/02/ 16/02/2013 21:00
但是,如果我执行new Date('16/02/2013 21:00').toString()
,它会给出'Wed Apr 02 2014 21:00:00 GMT+0200 (Hora de verano romance)'
。
我想这是因为我的日期不遵循 IETF RFC 2822 日期和时间规范。 然后,我应该转换我的字符串,我想将它转换为最相似的兼容格式(因为它应该更容易转换)。 但是http://tools.ietf.org/html/rfc2822#page-14很难理解,所以不知道哪个格式最相似。
是否有包含允许格式示例的列表?
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
还有一个跨浏览器不一致的矩阵。
参考
这个问题似乎是在问“ ECMAScript 实现需要解析哪些格式”。
在 ECMAScript Ed 5 (2011) 之前,解析完全依赖于实现。 ECMAScript 实现需要解析的格式可以概括为:
任何其他格式的解析仍然依赖于实现并且存在差异,因此一般规则是“不要使用内置解析器”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.