What is the “right” JSON date format?

前端 未结 16 1371
执念已碎
执念已碎 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 15:48

    When in doubt simply go to the javascript web console of a modern browser by pressing F12 (Ctrl+Shift+K in Firefox) and write the following:

    new Date().toISOString()
    

    Will output:

    "2019-07-04T13:33:03.969Z"

    Ta-da!!

    0 讨论(0)
  • 2020-11-21 15:52

    Just for reference I've seen this format used:

    Date.UTC(2017,2,22)
    

    It works with JSONP which is supported by the $.getJSON() function. Not sure I would go so far as to recommend this approach... just throwing it out there as a possibility because people are doing it this way.

    FWIW: Never use seconds since epoch in a communication protocol, nor milliseconds since epoch, because these are fraught with danger thanks to the randomized implementation of leap seconds (you have no idea whether sender and receiver both properly implement UTC leap seconds).

    Kind of a pet hate, but many people believe that UTC is just the new name for GMT -- wrong! If your system does not implement leap seconds then you are using GMT (often called UTC despite being incorrect). If you do fully implement leap seconds you really are using UTC. Future leap seconds cannot be known; they get published by the IERS as necessary and require constant updates. If you are running a system that attempts to implement leap seconds but contains and out-of-date reference table (more common than you might think) then you have neither GMT, nor UTC, you have a wonky system pretending to be UTC.

    These date counters are only compatible when expressed in a broken down format (y, m, d, etc). They are NEVER compatible in an epoch format. Keep that in mind.

    0 讨论(0)
  • 2020-11-21 15:54

    If you are using Kotlin then this will solve your problem. (MS Json format)

    val dataString = "/Date(1586583441106)/"
    val date = Date(Long.parseLong(dataString.substring(6, dataString.length - 2)))
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-21 16:00

    There is no right format; The JSON specification does not specify a format for exchanging dates which is why there are so many different ways to do it.

    The best format is arguably a date represented in ISO 8601 format (see Wikipedia); it is a well known and widely used format and can be handled across many different languages, making it very well suited for interoperability. If you have control over the generated json, for example, you provide data to other systems in json format, choosing 8601 as the date interchange format is a good choice.

    If you do not have control over the generated json, for example, you are the consumer of json from several different existing systems, the best way of handling this is to have a date parsing utility function to handle the different formats expected.

    0 讨论(0)
  • 2020-11-21 16:04

    In Sharepoint 2013, getting data in JSON there is no format to convert date into date only format, because in that date should be in ISO format

    yourDate.substring(0,10)
    

    This may be helpful for you

    0 讨论(0)
提交回复
热议问题