Javascript 使用 JSON.stringify() 和 JSON.parse() 时 Date() 的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11491938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:02:28  来源:igfitidea点击:

Issues with Date() when using JSON.stringify() and JSON.parse()

javascriptjsondate

提问by Roland

I am trying to calculate the difference between two times using JavaScript. It's just basic math but I seem to have some issues with that while using JSON.stringify()and JSON.parse().

我正在尝试使用 JavaScript 计算两次之间的差异。这只是基本的数学,但我在使用JSON.stringify()and 时似乎有一些问题JSON.parse()

If you're wondering why am I applying the JSON.stringify()function to the date, it's because I using local storage to store some data on the client side and use it whenever the client lands on my website again ( it's faster that way rather than making more requests to the server ). That data usually updates once in a while ( I'm grabbing the data through API from another website ), so I set up a data_updatevariable and I'm storing it together with the other data.

如果您想知道为什么我要将该JSON.stringify()函数应用于日期,那是因为我使用本地存储在客户端存储一些数据,并在客户端再次登陆我的网站时使用它(这样会更快,而不是发出更多请求)到服务器)。该数据通常会不时更新一次(我通过 API 从另一个网站获取数据),因此我设置了一个data_update变量并将其与其他数据存储在一起。

That way I'm grabbing the stored data from the local storage and check if the difference between data_update( which is a date / time ) and the time / date when the check it's made and see if it's greater than a week / day /etc .

这样我就从本地存储中获取存储的数据并检查data_update(这是一个日期/时间)和它进行检查时的时间/日期之间的差异,看看它是否大于一周/天/etc。

So that is the reason why I'm using the JSON functions. My problem is that when I'm parsing the data from the local storage, the date seems to be different from a Date()object.

这就是我使用 JSON 函数的原因。我的问题是,当我解析本地存储中的数据时,日期似乎与Date()对象不同。

I'm trying to do the next operation per say :

我正在尝试执行下一个操作:

var x = JSON.parse(JSON.stringify(new Date()));

var y = JSON.parse(this.get_local_storage_data(this.data_cache_key)); // the data object stored on local storage

var q = y.data_update; // this is the variable where the Date() was stored

console.log(Math.floor((x-q)/1000));

The above will return null. Also when I want to see the Math.floor(x)result, it returns nullagain.

以上将返回null。此外,当我想查看Math.floor(x)结果时,它会null再次返回。

So what can I do in this situation ? Is there a fix for this ?

那么在这种情况下我能做什么呢?有没有解决办法?

回答by Kyle Burton

If you look at the output of JSON.stringify for a Date, you'll see that:

如果您查看日期的 JSON.stringify 输出,您会看到:

JSON.stringify(new Date())

Results in a string. JSON does not have a primitive representation of Date objects that JSON.parse will turn back into a Date object automatically.

结果是一个字符串。JSON 没有 Date 对象的原始表示,JSON.parse 将自动转换回 Date 对象。

The Date object's constructor can take a date string, so you can turn those string values back into dates by doing:

Date 对象的构造函数可以接受一个日期字符串,因此您可以通过执行以下操作将这些字符串值转换回日期:

var x = new Date(JSON.parse(JSON.stringify(new Date())));

Then the arithmetic will work.

然后算术将起作用。

x = new Date(JSON.parse(JSON.stringify(new Date())))
y = new Date(JSON.parse(JSON.stringify(new Date())))
y - x
=> 982

回答by Lucio M. Tato

JSON.stringify(new Date())

returns

返回

"2013-10-06T15:32:18.605Z"

“2013-10-06T15:32:18.605Z”

Thank God is: Date.prototype.toISOString()

感谢上帝是: Date.prototype.toISOString()