rajat44 rajat44 - 3 months ago 52
Javascript Question

How to parse milliseconds to date in React-native

I am getting date from in this format "/Date(1471846675177+0000)/"

I have done this :

var longToDate=function(millisec){
var length=millisec.length-7;
var date=millisec.substring(6,length);
return (new Date(millisec.substring(6,length)).toUTCString());
}


But it is showing "invalid date".
Can someone tell me the reason for it ?

Answer

you forgot parseInt

var longToDate=function(millisec){
    var length=millisec.length-7;
    var date=parseInt(millisec.substring(6,length));
    return (new Date(date).toUTCString());
}
Comments