我将时间作为Unix时间戳存储在MySQL数据库中,并将其发送给一些JavaScript代码。我怎么才能把时间花在这上面呢?
例如,以HH/MM/SS
格式。
发布于 2009-05-11 08:28:51
let unix_timestamp = 1549312452
// 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(-2) + ':' + seconds.substr(-2);
console.log(formattedTime);
有关Date对象的更多信息,请参考MDN或ECMAScript 5 specification。
发布于 2011-05-21 08:44:36
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp * 1000);
var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
var year = a.getFullYear();
var month = months[a.getMonth()];
var date = a.getDate();
var hour = a.getHours();
var min = a.getMinutes();
var sec = a.getSeconds();
var time = date + ' ' + month + ' ' + year + ' ' + hour + ':' + min + ':' + sec ;
return time;
}
console.log(timeConverter(0));
发布于 2009-05-11 08:31:48
JavaScript以毫秒为单位工作,因此您必须首先将UNIX时间戳从秒转换为毫秒。
var date = new Date(UNIX_Timestamp * 1000);
// Manipulate JavaScript Date object here...
https://stackoverflow.com/questions/847185
复制相似问题