我想要一个表示当前日期和时间的数字,比如Unix时间戳。
当前回答
下面是一个生成时间戳的简单函数,格式为:mm/dd/yy hh:mi:ss
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())));
}
其他回答
jQuery提供了自己的方法来获取时间戳:
var timestamp = $.now();
(此外,它还实现了(newDate).getTime()表达式)
裁判:http://api.jquery.com/jQuery.now/
更简单的方法:
var timeStamp=event.timestamp || new Date().getTime();
在JavaScript中获取时间戳
在JavaScript中,时间戳是自1970年1月1日以来经过的毫秒数。如果您不打算支持<IE8,可以使用
new Date().getTime(); + new Date(); and Date.now();
直接获取时间戳,而无需创建新的Date对象。
返回所需的时间戳
new Date("11/01/2018").getTime()
var my_timestamp=~~(Date.now()/1000);
您只能使用
var timestamp=new Date().getTime();console.log(时间戳);
以获取当前时间戳。不需要做任何额外的事情。