猿问

用JavaScript将Unix时间戳转换为时间

用JavaScript将Unix时间戳转换为时间

我以Unix时间戳的形式将时间存储在MySQL数据库中,并将其发送给一些JavaScript代码。我怎么才能抽出时间呢?

例如,HH/MM/SS格式。


慕容森
浏览 1771回答 3
3回答

aluckdog

// 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 timestampvar hours = date.getHours();// Minutes part from the timestampvar minutes = "0" + date.getMinutes();// Seconds part from the timestampvar seconds = "0" + date.getSeconds();// Will display time in 10:30:23 formatvar formattedTime = hours + ':' + minutes.substr(-2) + ':' + seconds.substr(-2);

慕尼黑的夜晚无繁华

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));
随时随地看视频慕课网APP
我要回答