newUser newUser - 1 year ago 63
Javascript Question

Date function returns NaN if variable given as input

I am using google chrome and trying to build a messaging system. I need to display date/time of messages. I used date function but it is giving 'NaN' error. It works fine if I directly pass value, but with a variable input. It fails. I searched for the answer but I am unable to identify problem. below is my code.

var d = jsarr[j];// sets d = 1448702771436
alert(d); //1448702771436
var date = new Date(d);
alert(date); //invalid date

Answer Source

I tried parsing 'd', with parseInt() and now it works fine. @Andreas I even tried parsing well before but somehow it was not working back then. Thanks any ways.
Might be because of uncleared cache, I hard refreshed. using ctrl + F5

  var d = jsarr[i];   
        var s =  parseInt(d);//  changing type to Int 
          var date = new Date(s); // valid date