javascript tutorial - [Solved-5 Solutions] The “right” JSON date format - javascript - java script - javascript array



Problem:

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
click below button to copy the code. By JavaScript tutorial team

Which one is the right one? Or best? Is there any sort of standard on this?

Solution 1:

JSON itself does not specify how dates should be represented, but JavaScript does. We should use the format emitted by Date's toJSON method: 2012-04-23T18:25:43.511Z Here's why:

  • It's human readable but also succinct
  • It sorts correctly
  • It includes fractional seconds, which can help re-establish chronology
  • It conforms to ISO 8601
  • ISO 8601 has been well-established internationally for more than a decade
  • 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.

Solution 2:

JSON does not know anything about dates. What .NET does is a non-standard hack/extension.

WE would use a format that can be easily converted to a Date object in JavaScript, i.e. one that can be passed to new Date(...). The easiest and probably most portable format is the timestamp containing milliseconds since 1970.

Solution 3:

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 8601 format ; 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 we have control over the generated json, for example, we provide data to other systems in json format, choosing 8601 as the date interchange format is a good choice. If we do not have control over the generated json, for example, we 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.

Solution 4:

Just for reference I've seen this format used:

Date.UTC(2017,2,22)
click below button to copy the code. By JavaScript tutorial team

It works with JSONP which is supported by the $.getJSON() function. Not sure WE 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 (we 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 our system does not implement leap seconds then we are using GMT (often called UTC despite being incorrect). If we do fully implement leap seconds we really are using UTC. Future leap seconds cannot be known; they get published by the IERS as necessary and require constant updates. If we are running a system that attempts to implement leap seconds but contains and out-of-date reference table (more common than we might think) then we have neither GMT, nor UTC, we 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.

Solution 5:

It is work for me with parse Server { "ContractID": "203-17-DC0101-00003-10011", "Supplier":"Sample Co., Ltd", "Value":12345.80, "Curency":"USD", "StartDate": { "__type": "Date", "iso": "2017-08-22T06:11:00.000Z" } }


Related Searches to javascript tutorial - The “right” JSON date format