What is the “right” JSON date format?

前端 未结 16 1379
执念已碎
执念已碎 2020-11-21 15:14

I\'ve seen so many different standards for the JSON date format:

\"\\\"\\\\/Date(1335205592410)\\\\/\\\"\"         .NET JavaScriptSerializer
\"\\\"\\\\/Date(         


        
16条回答
  •  失恋的感觉
    2020-11-21 16:00

    JSON itself does not specify how dates should be represented, but JavaScript does.

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

    2012-04-23T18:25:43.511Z

    Here's why:

    1. It's human readable but also succinct

    2. It sorts correctly

    3. It includes fractional seconds, which can help re-establish chronology

    4. It conforms to ISO 8601

    5. ISO 8601 has been well-established internationally for more than a decade

    6. ISO 8601 is endorsed by W3C, RFC3339, and XKCD

    That being said, every date library ever written can understand "milliseconds since 1970". So for easy portability, ThiefMaster is right.

提交回复
热议问题