On Mar 20, 8:03 pm, Angelo Chen <angelochen...@gmail.com> wrote:
> when a json is json.stringify and parse back to json later, the date
> is different:

Right, because there is no special Date type in JSON, it has to
convert it to a string. When parsing that string back again, the JSON
parser can't assume any strings are necessarily a Date, so it just
keeps them as strings. You have to convert those manually. One way to
do this is to pass a callback to JSON.parse() as a second argument,
and it will execute the callback with the key and value as arguments.
Then it's just a matter of returning the original or transformed value
from that callback. You can also returned `undefined` to delete the
current value.

> var s = '{"id":1,"d":"2012-03-20T23:55:10.352Z"}';
undefined
> var o = JSON.parse(s, function(key, val) {
...   if (key === 'd') // or perhaps checking val using a regex
...     val = new Date(val);
...   return val;
... });
undefined
> o
{ id: 1, d: Tue, 20 Mar 2012 23:55:10 GMT }

-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nodejs@googlegroups.com
To unsubscribe from this group, send email to
nodejs+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to