gco
gco

Reputation: 1700

Get date from JS Timestamp

I have an array "data" which looks like

[1298214000000, "123456"] 

with

data[0] = 1298214000000

When trying to convert the timestamp of data[0] to a date I

EXPECT

Sun Feb 20 2011 16:00:00 GMT+0100

INSTEAD I GET

Mon Mar 24 2014 23:27:43 GMT+0100

Which is always the current date

I used

console.log(Date(data[0])

and also

console.log(Date(data[0].toString()));

None worked.

Upvotes: 1

Views: 154

Answers (1)

Yogu
Yogu

Reputation: 9445

Date is a constructor, so use it as one:

console.log(new Date(data[0]).toString());

new Date() will create a new Date object.

Simply calling the function results in a specific behaviour of Date - that is return the current date as a string (the string part documented in MDN, but I did not find specification for the current part).

Note that this is consistent with other primitive constructors:

new Boolean(): [object Boolean]
Boolean(): false

new String(): [object String]
String(): ""

Upvotes: 4

Related Questions