我将时间作为Unix时间戳存储在MySQL数据库中,并将其发送给一些JavaScript代码。我怎样才能抽出时间?
例如,HH/MM/SS格式。
我将时间作为Unix时间戳存储在MySQL数据库中,并将其发送给一些JavaScript代码。我怎样才能抽出时间?
例如,HH/MM/SS格式。
当前回答
现在您必须使用unix时间戳:
const dateTimeString = moment.unix(1466760005).format("DD-MM-YYYY HH:mm:ss");
其他回答
试试看:
new Date(1638525320* 1e3).toISOString() //2021-12-03T09:55:20.000Z
函数getDateTimeFromTimestamp(unixTimeStamp){let date=新日期(unixTimeStamp);return('0'+date.getDate()).slice(-2)+'/'+('0'+(date.getMonth()+1)).sslice(-2)+'/'+date.getFullYear()+''+('0'+date.getHours()).slice(-2')+':'+('0'+date.get-Minutes())slice(-2';}const myTime=getDateTimeFromTimestamp(1435986900000);console.log(myTime);//输出01/05/2000 11:00
Use:
var s = new Date(1504095567183).toLocaleDateString("en-US")
console.log(s)
// expected output "8/30/2017"
时间:
var s = new Date(1504095567183).toLocaleTimeString("en-US")
console.log(s)
// expected output "3:19:27 PM"
请参见Date.protype.toLocaleDateString()
参见日期/年代转换器。
您需要ParseInt,否则它将无法工作:
if (!window.a)
window.a = new Date();
var mEpoch = parseInt(UNIX_timestamp);
if (mEpoch < 10000000000)
mEpoch *= 1000;
------
a.setTime(mEpoch);
var year = a.getFullYear();
...
return time;
让unix_timestamp=1549312452//基于时间戳创建新的JavaScript Date对象//乘以1000,使参数以毫秒为单位,而不是以秒为单位。var date=新日期(unix_timestamp*1000);//时间戳的小时部分var hours=date.getHours();//时间戳的分钟部分var minutes=“0”+date.getMinutes();//时间戳的秒部分var seconds=“0”+date.getSeconds();//将以10:30:23格式显示时间var formattedTime=hours+‘:‘+minutes.substr(-2)+‘:’+seconds.substr(-3);console.log(格式化时间);
有关Date对象的更多信息,请参阅MDN或ECMAScript 5规范。