什么是“权利"?JSON日期格式?

2022-01-31 00:00:00 json javascript

我见过很多不同的 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.

您应该使用 DatetoJSON 方法:

You should use the format emitted by Date's toJSON method:

2012-04-23T18:25:43.511Z

原因如下:

  1. 它既可读又简洁

  1. 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.

相关文章