我想要一个表示当前日期和时间的数字,比如Unix时间戳。
当前回答
有时我需要在xmlhttp调用的对象中使用它,所以我喜欢这样做。
timestamp : parseInt(new Date().getTime()/1000, 10)
其他回答
有很多方法可以做到这一点。
Date.now()
new Date().getTime()
new Date().valueOf()
要获取以秒为单位的时间戳,请使用以下方法进行转换:
Math.floor(Date.now() / 1000)
var timestamp = Number(new Date()); // current time as number
您只能使用
var timestamp=new Date().getTime();console.log(时间戳);
以获取当前时间戳。不需要做任何额外的事情。
function getTimeStamp() {
var now = new Date();
return ((now.getMonth() + 1) + '/' +
(now.getDate()) + '/' +
now.getFullYear() + " " +
now.getHours() + ':' +
((now.getMinutes() < 10)
? ("0" + now.getMinutes())
: (now.getMinutes())) + ':' +
((now.getSeconds() < 10)
? ("0" + now.getSeconds())
: (now.getSeconds())));
}
这一个有一个解决方案:在js中将unixtime stamp转换为tim
var a = new Date(UNIX_timestamp*1000);
var hour = a.getUTCHours();
var min = a.getUTCMinutes();
var sec = a.getUTCSeconds();