1

i'm needing my json.stringify method to return me something like this:

//The code below is the same as JSON.stringify(new Date());
console.log(new Date().toJSON());

This returns me the following:

"/Date(1373046760480-0300)/"

which is fine, but it happens in certain scenarios that the same operation returns me this:

"2013-07-05T17:52:55.434Z"

which is not the expected result, and then i have to create string to have the expected result.

Does anybody know why this happens?

9
  • 1
    What circumstances? As far as I can tell, it always returns the latter string. Commented Jul 5, 2013 at 18:02
  • 3
    ??? In what scenario in what browser do you ever get the first string? Commented Jul 5, 2013 at 18:02
  • For me console.log(new Date().toJSON()) returns "2013-07-05T18:05:10.600Z" in Chrome and Firefox Commented Jul 5, 2013 at 18:06
  • The first is not valid JSON. Commented Jul 5, 2013 at 18:07
  • 2
    According to the specification, toJSON should call toISOString (es5.github.io/#x15.9.5.44). Commented Jul 5, 2013 at 18:08

1 Answer 1

4

Unless someone accidentally (by using third party library) overwrites Date.prototype.toJSON the default output of JSON date is the latter:

Here's what could be (a non-standard implementation used by asp.net)

Date.prototype.toJSON = function(){ return "/Date(" + this.getTime() + ")/" }
Sign up to request clarification or add additional context in comments.

1 Comment

That is the case, the sencha framework does this... thanks i started thinking my knowledge of dates in js was rusty!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.