1

I'm getting very strange error in JavaScript.

var stamp = 1349102;
var obj = {a: stamp, b: new Date(stamp), c: new Date(1349102)};

When I look into obj - b says invalid Date but c is valid Date object.

Please help me. I really don't know how to solve this issue.

4
  • 2
    did you mean the final ) to be there? Commented Feb 24, 2013 at 20:19
  • Works for me, without the extra left paren: obj.b.toString() === obj.c.toString() Commented Feb 24, 2013 at 20:23
  • well, the stamp var isnt constant, its value comes from input Commented Feb 24, 2013 at 20:26
  • Works for me too : jsfiddle.net/UQTY2/27 Commented Feb 24, 2013 at 20:41

1 Answer 1

1

If stamp is provided by a user, it can be considered as string. In that case your code would be interpreted by a browser as the following code (which does not work):

var stamp = "1349102";
var obj = {a: stamp, b: new Date(stamp), c: new Date(1349102)};

Convert stamp to Number and it will be fine

var stamp = "1349102";
var obj = {a: stamp, b: new Date(Number(stamp)), c: new Date(1349102)};

See the console output: first is without Number conversion, second is with Number conversion.

JSON Console Output

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this was the problem ;)

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.