我需要在JavaScript中解析日期.格式是
[2 digits day]/[2 digits month]/[4 digits year] [2 digits hour (24 mode)]:[2 digits minute]
例如,16/02/2013 21:00
但如果我做新的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
陷阱
还有一个cross-browser inconsistencies的矩阵.
参考
> Same Markup: Writing Cross-Browser Code – IEBlog
> Loading Javascript files in parallel – Kristoffer’s tidbits