Reputation: 597
I am trying to convert milliseconds (which i am getting from an api which returns JSN data) to a readable date format. The sample code is below -
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var d = new Date(Number("1429894800"));
document.getElementById("demo").innerHTML = d.toString();
</script>
</body>
</html>
It returns - Sat Jan 17 1970 07:11:34 GMT-0600 (CST). But it should be the current date time. How to do the conversion?
Upvotes: 1
Views: 55
Reputation: 870
If you want, approximately, the time "now" you would need to use:
var d = new Date(1429894800000);
(Notice I removed the Number("...")
because you don't need to convert a string to a number when you can just use a number.)
which outputs, for me:
Date {Fri Apr 24 2015 13:00:00 GMT-0400 (EDT)}
But a better way to determine "now" is using:
Date.now();
For lots more information, go to a handy dandy page on the Date
class here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
Upvotes: 2