我将时间作为Unix时间戳存储在MySQL数据库中,并将其发送给一些JavaScript代码。我怎样才能抽出时间?
例如,HH/MM/SS格式。
我将时间作为Unix时间戳存储在MySQL数据库中,并将其发送给一些JavaScript代码。我怎样才能抽出时间?
例如,HH/MM/SS格式。
当前回答
下面的代码还提供了3位毫秒,非常适合控制台日志前缀:
const timeStrGet=日期=>{const milliSecsStr=date.getMilliseconds().toString().padStart(3,“0”);返回`${date.toLocaleTimeString('it-US')}.${milliSecsStr}`;};setInterval(()=>console.log(timeStrGet(new Date())),299);
其他回答
下面的代码还提供了3位毫秒,非常适合控制台日志前缀:
const timeStrGet=日期=>{const milliSecsStr=date.getMilliseconds().toString().padStart(3,“0”);返回`${date.toLocaleTimeString('it-US')}.${milliSecsStr}`;};setInterval(()=>console.log(timeStrGet(new Date())),299);
函数timeConverter(UNIX_timestamp){var a=新日期(UNIX_timestamp*1000);var months=['Jan','Feb','Mar','Pr','May','Jun','Jul','Aug','Step','Oct','Nov','Dec'];var year=a.getFullYear();var month=月[a.getMonth()];var date=a.getDate();var hour=a.getHours();var最小值=a.获取最小值();var sec=a.getSeconds();var time=日期+“”+月份+“”+year+“”“+小时+”:“+分钟+”:”+秒;返回时间;}console.log(timeConverter(0));
注意一些答案的零问题。例如,时间戳1439329773将错误地转换为2015年8月12日0:49。
我建议使用以下方法来解决这个问题:
var timestamp = 1439329773; // replace your timestamp
var date = new Date(timestamp * 1000);
var formattedDate = ('0' + date.getDate()).slice(-2) + '/' + ('0' + (date.getMonth() + 1)).slice(-2) + '/' + date.getFullYear() + ' ' + ('0' + date.getHours()).slice(-2) + ':' + ('0' + date.getMinutes()).slice(-2);
console.log(formattedDate);
现在的结果是:
12/08/2015 00:49
最短的
(new Date(ts*1000)+'').slice(16,24)
设ts=1549312452;让时间=(新日期(ts*1000)+“”).切片(16,24);console.log(时间);
function timeConverter(UNIX_timestamp){
var a = new Date(UNIX_timestamp*1000);
var hour = a.getUTCHours();
var min = a.getUTCMinutes();
var sec = a.getUTCSeconds();
var time = hour+':'+min+':'+sec ;
return time;
}