3

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?

2
  • 4
    Isn't Unix timestamp in seconds?
    – Teemu
    Commented Aug 8, 2017 at 13:04
  • Date.now() (eg 1502197442246) has three additional digits. Try multiplying by 1000...
    – Ben Aston
    Commented Aug 8, 2017 at 13:04

2 Answers 2

14

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>

0
-1

<p id="demo"></p>

<script>
  var d = new Date(1501783442);
  document.getElementById("demo").innerHTML = d;
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.