开发者

How (or more appropriately: Why) does JSON.Stringify change the Date() value?

开发者 https://www.devze.com 2023-01-11 11:34 出处:网络
See example of the issue here: http://www.jsfiddle.net/s2uYE/ Notice the second new Date() is 1hr behind the first new Date() even though they are initiated at the same time.

See example of the issue here: http://www.jsfiddle.net/s2uYE/

Notice the second new Date() is 1hr behind the first new Date() even though they are initiated at the same time.

This caused me so end of confusion in a recent project and I would just like to know why this happens and if this is a bug in how browsers handle dates when stringified via JSON or whether this is 'supposed' to开发者_如何学JAVA happen and I just haven't realised the benefit yet.

Any help appreciated!

Kind regards, Mark


I would wager that this is expected behavior. Consider that the world wide web is truly "world wide", and that you may have a time or date input on a form. If you generate this value from Javascript, most of the time you don't want the local time relative to the user's browser. When I go to your fiddle, I'm 4 hours behind the JSON time.

On the server side, you would want to distinguish this time input in the most neutral way possible. GMT is a very convenient standard. Remember, it can be midnight somewhere in the world, but it will only be midnight GMT once.


alert(new Date().toUTCString() + '\n' + JSON.stringify({ 'date': new Date() }));


Sorry, I'm a still a little confused as to how the Date() value is changing and where.

If I call 'new Date()' the browser software takes the time from my computer, correct?

If I was to call 'new Date()' from within the JSON.Stringify() method, what happens then? Where does new Date() get it's value, I thought it would still be from my machine which should then be the same value as the first 'new Date()'?

If that wasn't the case and calling 'new Date()' from within JSON.Stringify somehow pulled a different timezone then surely it could be worked around by storing the 'new Date()' value in a variable and then placing this variable in the object literal which is passed to JSON.Stringify?

e.g. (sorry I'm only allowed to post 1 link so had to place my jsFiddle code here)

var storeDate = new Date();
alert(storeDate + '\n' + JSON.stringify({ 'date': storeDate })); ​

But that doesn't work, so it seems that JSON.Stringify does somehow change the variable?

Sorry if I've mis-understood what people are suggesting. But it sounds like you are saying it all depends on timezones - which is fine - but I just don't get why executing 'new Date()' and storing the value within a variable can then have it's set value changed to a different value when passed through JSON.Stringify?

UPDATE: so it seems even though I'm storing new Date() in a variable it is NOT a set value stored in the variable. http://www.jsfiddle.net/s2uYE/3/ shows that if I manually type out the datetime then JSON.Stringify does nothing.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号