-2

When I create a new object var date = new Date(1550571050) this returns

Mon Jan 19 1970 05:42:51 GMT+0700

while it should be

Mon Feb 19 2019 05:42:51 GTM+0700

whats wrong with it?

jo_va
  • 13,504
  • 3
  • 23
  • 47
My real name
  • 535
  • 1
  • 5
  • 17

4 Answers4

9

var date = new Date(1550571050) has date 1550571050 which is in seconds.

As described on MDN, Date constructor accepts a unix timestamp which is

an integer value representing the number of milliseconds

If you add 000 to your date it will be in milliseconds instead of seconds and the date object will be correct

new Date(1550571050000)
// Tue Feb 19 2019 12:10:50 GMT+0200 (Eastern European Standard Time)
Vlad Holubiev
  • 4,876
  • 7
  • 44
  • 59
  • @Myrealname if you find some answer on Stackoverflow solves your problem - click a checkmark icon under the voting buttons so it helps other users – Vlad Holubiev Feb 19 '19 at 10:34
1

in Javascript timestamps are given in milliseconds, not seconds

Alb
  • 1,063
  • 9
  • 33
1

Just do like this:

var timestamp = 1550571050;
var date = new Date(timestamp * 1000);

And refer this:

Timestamp to human readable format

Kinjal
  • 91
  • 2
  • 9
1

timestamps are in milliseconds so multiply by 1000. then it will give correct date.

new Date(1550571050000)
Mohit Prakash
  • 492
  • 2
  • 7
  • 15