user6198555
user6198555

Reputation:

Date Formatting String

Ok guys, you may think this is a duplicate but its not... I got a very wierd result. So im currently making a fourum in AngularJS , and when i logged the date the user created a topic, it gave me this:

1460742376663

I was like ok maybe if i do date.getFullYear(); it would work.. But guess what any methods didnt work. Well then i understood why because when i executed this statement

console.log(typeof date)

It gave me a string... So then i was in the look for how to convert string to date. And i found it so i tried this:

var newDateFormat = new Date(date)

To try to convert it to an Object Date. But Guess what.... when i logged that newDateFormat it gave me Invalid Date.. So now i have no clue what to do. Please Help! I will be out somewhere, so i wont see anwser soon. Maybe it works with an algoithm? But i checked that to by trying to find the year in that date number :

1460742376663

I didnt find... Please help. As i said i am leaving now i will see anwser later!

Upvotes: 0

Views: 39

Answers (2)

alemures
alemures

Reputation: 116

The Date constructor receives an ISO date String or a timestamp since epoch Number. Because your value is in String format, it needs to be converted to a Number before you can use it as a timestamp.

var date = '1460742376663';
var newDateFormat = new Date(parseInt(date));

Upvotes: 0

wrldbt
wrldbt

Reputation: 191

It returned a Unix Timestamp to you. If you want to see that it is in fact a valid date check on http://www.unixtimestamp.com/index.php. That number represents the number of milliseconds since Jan 01 1970. (UTC).

You can use a library like moment.js to parse and format this for you.

Here is the documentation for that:

http://momentjs.com/docs/#/parsing/unix-timestamp-milliseconds/

var day = moment(1318781876406);

Upvotes: 1

Related Questions