That’s a fairly long title to describe this:
I found this value in a field of a JSON response from a web service where I was expecting a date. I’m used to seeing 13 digit timestamps so this date surprised me and looked a little odd.
After some research I found that the 13-digit timestamp that I’ve grown accustomed to seeing and this 10-digit timestamp are both Unix-style timestamps that represent the number of seconds since January 1, 1970 at 00:00:00 GMT. The difference is that the 13-digit timestamps represent the number of milliseconds and the 10-digit timestamps represent the number of seconds.
var timestamp = 1280296860145; var pubDate = new Date(timestamp);
var pubDate = new Date(); pubDate.setTime(timestamp);
But neither of those worked with the 10-digit timestamp, which seems pretty obvious now that I know that the constructor and the setTime method expect milliseconds. So when you are given a 10-digit timestamp you should first convert it from seconds to milliseconds by multiplying by 1000:
var timestamp = 1280296860; var pubDate = new Date(timestamp * 1000); //expects milliseconds
var pubDate = new Date(); pubDate.setTime(timestamp * 1000); //expects milliseconds
var weekday=new Array("Sun","Mon","Tue","Wed","Thu","Fri","Sat"); var monthname=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"); var formattedDate = weekday[pubDate.getDay()] + ' ' + monthname[pubDate.getMonth()] + ' ' + pubDate.getDate() + ', ' + pubDate.getFullYear()