0

I want to convert systematic date into readable date format. However when I pass systematic date as argument to date constructor I get Invalid date response. How to do this properly in order to display formatted date such as dd-mm-yyyy for GMT+2 ?

var date = message.date; // => 1466663308000
        var dateObject = new Date(date);
        console.log(dateObject);

Console output:

Invalid Date

Kunok
  • 5,543
  • 8
  • 38
  • 72

2 Answers2

1

You have to make sure the timestamp value is a number and not a string:

    var date = message.date; 
    var dateObject = new Date(+date); // note the +
    console.log(dateObject);

Once you've got a valid date, there are many other questions here about formatting dates.

Community
  • 1
  • 1
Pointy
  • 371,531
  • 55
  • 528
  • 584
0

I tried the code, it is absolutely correct. I can get the correct date

var d=new Date(1466663308000);
document.write(d);

But I tried another way:

var x = "1466663308000";
var d=new Date(x);
document.write(d);

I got the "Invalid date", so I guess, message.date should be a string, please try to long(message.date).