简体   繁体   中英

JSON ISO8601 parsing in JavaScript

Still learning the nuances of JavaScript date handling with what looks like a bug somewhere.

Returning records from C# using .ToUniversalTime()... works like a charm, however, JavaScript balks at some of the date/time coming back.

The good: 2012-12-14T21:25:44.273Z toLocaleTimeString() returns 2:25:44 PM

the bad: 2012-12-14T21:25:44.18Z returns Invalid Date

the ugly: what with the .18Z at the end... should it have been .018Z or .180Z? And, is it a C# bug or a JavaScript bug?

Yes, it is an IE9 bug, and it does work in IE10. However, you can use Moment.js to make this work consistently cross-browser, and yes - it does work in IE9.

// This works in IE10 and Chrome, fails in IE9
alert(new Date("2012-12-14T21:25:44.18Z"));


// This works everywhere
alert(moment("2012-12-14T21:25:44.18Z"));

使用Date.parse解析ISO- Date.parse

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM