“正确的” JSON日期格式 [英] The "right" JSON date format
问题描述
我见过很多不同的JSON日期格式标准:
I've seen so many different standards for the JSON date format:
"\"\\/Date(1335205592410)\\/\"" .NET JavaScriptSerializer
"\"\\/Date(1335205592410-0500)\\/\"" .NET DataContractJsonSerializer
"2012-04-23T18:25:43.511Z" JavaScript built-in JSON object
"2012-04-21T18:25:43-05:00" ISO 8601
哪一个是正确的?还是最好的?对此有什么标准吗?
Which one is the right one? Or best? Is there any sort of standard on this?
推荐答案
JSON 本身不指定日期应如何表示,但JavaScript确实如此。
JSON itself does not specify how dates should be represented, but JavaScript does.
你应使用 日期
的MDN文档 toJSON
方法的MDN文档:
You should use the format emitted by Date
's toJSON
method:
2012-04-23T18:25:43.511Z
原因如下:
-
这是人类可读但又简洁
It's human readable but also succinct
排序正确
它包括小数秒,可以帮助重建年表
It includes fractional seconds, which can help re-establish chronology
符合 ISO 8601
ISO 8601已在国际上建立了十多年
ISO 8601 has been well-established internationally for more than a decade
ISO 8601由 W3C 认可, RFC3339 ,以及 XKCD
ISO 8601 is endorsed by W3C, RFC3339, and XKCD
那就是说,所写的每个日期库都可以理解自1970年以来的毫秒。所以为了便于携带,ThiefMaster是对的。
That being said, every date library ever written can understand "milliseconds since 1970". So for easy portability, ThiefMaster is right.
这篇关于“正确的” JSON日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!