0

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?

2
  • 1
    When you use the timestamp without multiplying it by a thousand (i.e. 1481791797) JavaScript will give you "Dec 15, 2016". Is that not what you wanted? If you want it to give you "July 14, 2016" for that timestamp you're out of luck no matter what language you use... Commented Dec 21, 2016 at 12:36
  • @moopet yes you are correct. It works now.thank you. Commented Dec 21, 2016 at 13:34

1 Answer 1

1

You should use a library like moment.js

http://momentjs.com/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.