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)
.
- 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 asJSON.stringify
does. – Bergi Commented Sep 12, 2014 at 11:35
1 Answer
Reset to default 3JSON.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
版权声明:本文标题:javascript - how to get date with JSON.stringify(data) in angularjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745647307a2668213.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论