David Hope David Hope - 15 days ago 4
Javascript Question

Javascript: Convert timestamp to human readable date?

I'm simply trying to convert a timestamp back into a human readable date but what I get when converted is strange and wrong.

This is how I save the timestamp:

var timestamp = Number(new Date());
localStorag.setItem("mytimestamp", timestamp);


and this is how I get it back and convert it to readable date:

var mydate = localStorag.getItem("mytimestamp");
var jsDate = new Date(mydate*1000);

alert(jsDate);


The
jsDate
is wrong and I don't understand what's causing it!

Could someone please advise on this?

Answer

To solve your problem is just don't multiply it with 1000.

This works for me.

var a = Number(new Date()) //1479895361931

Then

date = new Date(a) //Wed Nov 23 2016 18:03:25 GMT+0800 (WITA)

I got correct time.