0

Getting strange results when converting a timestamp to readable date.

var date = new Date(x);

console.log(x);

showTooltip(item.pageX, item.pageY, (date.getMonth()+1)+"-"+date.getDate()+"-"+date.getFullYear()+" / "+y );

The console shows for example '1404792000000' so the timestamp is good, but the output results in 'NaN-NaN-NaN / 864'.

What am I doing wrong here?

user2226755
  • 12,494
  • 5
  • 50
  • 73
user756659
  • 3,372
  • 13
  • 55
  • 110
  • Not enough code is presented. Where do x, item.pageX, item.pageY all come from and are they correct, and are they the arguments wanted by showTooltip() function. You are correct x is a number. – Wayne Jul 13 '14 at 08:05
  • Reason: `new Date('1404792000000')` -> Invalid Date, so all the accessors return NaN – user2864740 Jul 13 '14 at 08:16
  • @user2864740 I'm assumeing the '' are not actually part of the value of x. If it were then the source would be something like x = "'1404792000000'"; or a string with quotes in the string. – Wayne Jul 13 '14 at 08:26
  • @Wayne Any string which is not recognized/supported as per Date.parse will result in such an "Invalid Date" object, it need not contain quotes in it although that is a pretty good way to make it invalid. – user2864740 Jul 13 '14 at 08:49

2 Answers2

3

Verify x with typeof function like this :

console.log(typeof x);

X must to be an number (int) if is an timestamp (like this 1404792000000), use parseInt() function if x is a string.

var date = new Date(parseInt(x));

showTooltip(item.pageX, item.pageY, (date.getMonth()+1)+"-"+date.getDate()+"-"+date.getFullYear()+" / "+y );

Example :

date = new Date("1404792000000"); // String : Date {Invalid Date}

date = new Date(1404792000000); // Integer : Date {Tue Jul 08 2014 ...}

date = new Date(parseInt("1404792000000")); // Str to int : Date {Tue Jul 08 2014 ...}

Without parseInt, use the calculator operator. (@RobG comment)

Like this :

date = new Date(+"1404792000000"); // Str to int

Which is better to use for a calculator parseInt() or eval() in Javascript?

Community
  • 1
  • 1
user2226755
  • 12,494
  • 5
  • 50
  • 73
  • 1
    "X must to be an integer" is a bit misleading. I think you meant to say "to be treated as a [*time value*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.1.1), *x* must be a String", otherwise it will be passed to [*Date.parse*](http://ecma-international.org/ecma-262/5.1/#sec-15.9.4.2) which will attempt to parse it as a date and time. Since "1404792000000" is not a valid date and time string, *Date.parse* will return `NaN`. It's not really necessary to test the value, just apply parseInt anyway (or to be more concise, use the unary `+` operator: `d = new Date(+x)`). – RobG Jul 13 '14 at 08:32
  • Thank you for your constructive comment. – user2226755 Jul 13 '14 at 08:42
1

The reason is that new Date('1404792000000') results in an "Invalid Date" object, so all the access methods (e.g. getMonth) return NaN. This is because the date constructor does not accept a string representing the epoch milliseconds.

Compare with new Date(1404792000000), which is "Mon Jul 07 2014 21:00:00 GMT-0700 (Pacific Daylight Time)". Note how a number was specified.

user2864740
  • 60,010
  • 15
  • 145
  • 220