JSON. stringify: the date and time are inaccurate when JSON is converted. json. stringifyjson

Source: Internet
Author: User
Tags tojson

JSON. stringify: the date and time are inaccurate when JSON is converted. json. stringifyjson

Call JSON. when stringify converts an object to a corresponding string, if it contains a time object, the time object will be converted to the national standard time (ISO) instead of the time of the current country region. The test code is as follows:

Copy codeThe Code is as follows:
<Script>
// Var o = new Date ();
// Console. log (o. toString () // China time zone, in the format of "Wed Jun 11 2014 10:51:42 GMT + 0800"
// Console. log (JSON. stringify (o); // output the International Standard Time (ISO), which is reduced by 8 hours, for example, "2014-06-11T02: 51: 42.624Z"
</Script>

JSON. stringify converts the Date object to the time zone of the current country, instead of the international standard time. You can rewrite the toJSON method of the prototype of the Date object and return the custom time format because of JSON. stringify calls the toJSON method of the Date object, for example:

Copy codeThe Code is as follows:
<Script>
Date. prototype. toJSON = function () {return this. toLocaleString ();}
Var o = new Date ();
Console. log (o. toString () // default format: "Wed Jun 11 2014 10:51:42 GMT + 0800"
Console. log (JSON. stringify (o); // output the custom local time: 10:57:27, January 1, June 11, 2014"
</Script>


How to Use JSONstringify () in js?

No problems.

Perform some tests
Alert ('json object created successfully'); // if you cannot see this, the error alert ('browser '+ (window. JSON? ''| 'Non') + JSON supported); alert (JSON. stringify (submit ));

In java, the list is converted to json format. After conversion, what should I do if the time format is incorrect?

The format may not be 2012-01-01 when you use JDBC for query.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.