gitu
gitu

Reputation: 219

Convert Unix time stamp to readable format not working in JavaScript code

I was trying to convert a timestamp to a format(July 14, 2016) and I used the following code:

    var wiDaterawa = entry.data.channel.created;
    var wiDateraw = new Date(entry.data.channel.created * 1000);    
    var months =  
    ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
    var year = wiDateraw.getFullYear();
    var month = months[wiDateraw.getMonth()];
    var date = wiDateraw.getDate();
    var wiDate = month + ' ' + date + ', '  + year ;

By this code I tried to pass the timestamp: 1481791797000 and got the result as:

Feb 11, 48926

The year is not showing in proper manner. When I tested it with:

var wiDate = new Date(entry.data.channel.created);

It showed :

Thu Dec 15 2016 12:49:57 GMT+0400 (Arabian Standard Time)

Also I tried it with php code:

 <?php print $returnValue = date('M d,Y', 1481791797000); ?>

Got the result:

Feb 11, 48926

I need the JavaScript based result as I am binding all the result in the a JavaScript function.

Could you please help me to sort it out?

Upvotes: 0

Views: 110

Answers (1)

olikaf
olikaf

Reputation: 601

You should use a library like moment.js

http://momentjs.com/

Upvotes: 1

Related Questions