Reputation: 37172
I am using Date
to convert unix milliseconds epoch to a date.
So 1501783442 == Tuesday, 8 August 2017 12:35:57
.
But javascript says its
Sun Jan 18 1970 14:39:43 GMT+0530 (IST)
.
<p id="demo"></p>
<script>
var d = new Date(1501783442);
document.getElementById("demo").innerHTML = d;
</script>
Jsfiddle link.
Whats going on here?
Upvotes: 3
Views: 2550
Reputation: 1
<p id="demo"></p>
<script>
var d = new Date(1501783442);
document.getElementById("demo").innerHTML = d;
</script>
Upvotes: -1
Reputation: 24137
First of all, 1501783442
equals GMT: Thursday, August 3, 2017 6:04:02 PM
according to https://www.epochconverter.com/.
Second of all, Unix uses seconds whereas Javascript uses milliseconds. So in order to convert, you must multiply by 1000, which then gives the correct result (corrected for the timezone that your browser lives in):
<p id="demo"></p>
<script>
var d = new Date(1501783442 * 1000);
document.getElementById("demo").innerHTML = d;
</script>
Upvotes: 14