jbutko
1/18/2015 - 8:15 PM

#JS, #JavaScript: Convert a Unix timestamp to human readable time

#JS, #JavaScript: Convert a Unix timestamp to human readable time

// create a new javascript Date object based on the timestamp
// multiplied by 1000 so that the argument is in milliseconds, not seconds
var date = new Date(unix_timestamp*1000);
// hours part from the timestamp
var hours = date.getHours();
// minutes part from the timestamp
var minutes = "0" + date.getMinutes();
// seconds part from the timestamp
var seconds = "0" + date.getSeconds();

// will display time in 10:30:23 format
var formattedTime = hours + ':' + minutes.substr(minutes.length-2) + ':' + seconds.substr(seconds.length-2);

// From http://stackoverflow.com/questions/847185/convert-a-unix-timestamp-to-time-in-javascript