admin管理员组

文章数量:1435090

In my angular js application i got success response from web service ,and i am trying to displaying the response data in console

console.log(JSON.stringify(data)) 

But the problem is in my success response data has a date element .i have printed the before and after the JSON.stringify(data) the outputs are : before :Tue Aug 14 2012 00:00:00 GMT+0530 (India Standard Time) after JSON.stringify(data) :"2012-08-13T18:30:00.000Z"

in after it shows 13 Aug date instead of 14 Aug date .

please tell me how can i solve this issue,i want to get 14 aug after JSON.stringify(data) .

In my angular js application i got success response from web service ,and i am trying to displaying the response data in console

console.log(JSON.stringify(data)) 

But the problem is in my success response data has a date element .i have printed the before and after the JSON.stringify(data) the outputs are : before :Tue Aug 14 2012 00:00:00 GMT+0530 (India Standard Time) after JSON.stringify(data) :"2012-08-13T18:30:00.000Z"

in after it shows 13 Aug date instead of 14 Aug date .

please tell me how can i solve this issue,i want to get 14 aug after JSON.stringify(data) .

Share Improve this question asked Sep 12, 2014 at 11:03 ShekkarShekkar 2443 gold badges10 silver badges22 bronze badges 4
  • 1 Seems like both your dates are in different format and different timezones. The first one in GMT+5:30 while the second one is GMT+0. ALso add more code definitely dates are not being printed by your console.log statements – Sap Commented Sep 12, 2014 at 11:07
  • 2 What's wrong? Both represent the exactly same datetime. – Bergi Commented Sep 12, 2014 at 11:09
  • @Bergi could you please tell me how to get the same format as before json.stingify(data). – Shekkar Commented Sep 12, 2014 at 11:15
  • Before stringifying there is no format, there is just time. You can either use .toString on it to get a representation of it in the local timezone, or you use .toUTCString() to get the same representation as JSON.stringify does. – Bergi Commented Sep 12, 2014 at 11:35
Add a ment  | 

1 Answer 1

Reset to default 3

JSON.stringify uses the toJSON method - if defined - to serialize objects. Date objects have that method defined in their prototype indeed, and it's pretty much equivalent to Date.prototype.toISOString.

So a solution would be redefining Date.prototype.toJSON: I don't really remend it, as the ISO format is pretty much conventional to transport dates as strings, but it can be done.

This example makes use of moment.js:

Date.prototype.toJSON = function() {
    return moment(this).format("YYYY-MM-DD HH:mm:ss");
};

In the end, ask yourself: why should you use JSON.stringify to display a date on the console? Date has plenty of methods to display dates (toString, toUTCString, toLocaleString, toISOString, ... not all of them supported by every browser, to be fair), and if they don't satisfy your needs there are quite some date libraries (like moment.js mentioned above) that can help you.

本文标签: javascripthow to get date with JSONstringify(data) in angularjsStack Overflow