使用JSON.stringify()和JSON.parse()时Date()的问题 [英] Issues with Date() when using JSON.stringify() and JSON.parse()

查看:1068
本文介绍了使用JSON.stringify()和JSON.parse()时Date()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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().

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

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_update variable and I'm storing it together with the other data.

这样我就从本地存储器中获取存储的数据,并检查 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 .

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

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.

我正在尝试下一个操作,说:

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));

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

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

那么在这种情况下我该怎么办?是否有修复?

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

推荐答案

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

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

JSON.stringify(new Date())

结果字符串。 JSON不具有Date对象的原始表示形式,JSON.parse将自动返回到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.

Date对象的构造函数可以采用日期字符串,所以你可以通过执行以下操作将这些字符串值转回日期:

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

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

然后算术工作。

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

这篇关于使用JSON.stringify()和JSON.parse()时Date()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆