Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with Date() when using JSON.stringify() and 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().

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.

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 .

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

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 ?

like image 802
Roland Avatar asked Jul 15 '12 12:07

Roland


People also ask

What is the difference between JSON parse and JSON Stringify?

The JSON. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.

Does JSON Stringify convert date to UTC?

stringify converts DateTimeOffset to UTC format #1710.

Is it bad to use JSON Stringify?

It`s ok to use it with some primitives like Numbers, Strings or Booleans. As you can see, you can just lose unsupported some data when copying your object in such a way. Moreover, JavaScript won`t even warn you about that, because calling JSON. stringify() with such data types does not throw any error.

How dates and functions are parsed in JSON?

Use the toJSON() method to get a string representation of the Date object. Pass the JSON string to the Date() constructor. The Date() constructor will parse the ISO string and will create a Date object.


2 Answers

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

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.

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

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 
like image 199
Kyle Burton Avatar answered Sep 23 '22 22:09

Kyle Burton


JSON.stringify(new Date()) 

returns

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

Thank God is: Date.prototype.toISOString()

like image 40
Lucio M. Tato Avatar answered Sep 21 '22 22:09

Lucio M. Tato