David Hope David Hope - 1 year ago 130
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);


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

Could someone please advise on this?

Answer Source

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

This works for me.

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


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

I got correct time.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download