我想要一个表示当前日期和时间的数字,比如Unix时间戳。
当前回答
更简单的方法:
var timeStamp=event.timestamp || new Date().getTime();
其他回答
这一个有一个解决方案:在js中将unixtime stamp转换为tim
var a = new Date(UNIX_timestamp*1000);
var hour = a.getUTCHours();
var min = a.getUTCMinutes();
var sec = a.getUTCSeconds();
time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);
我喜欢这个,因为它很小:
+new Date
我也喜欢这个,因为它同样短,而且与现代浏览器兼容,超过500人投票认为它更好:
Date.now()
JavaScript的工作时间是从纪元开始的毫秒数,而大多数其他语言的工作时间都是秒。您可以使用毫秒来工作,但只要您传递一个值来表示PHP,PHP本机函数可能就会失败。所以,为了确保我总是使用秒,而不是毫秒。
这将为您提供Unix时间戳(以秒为单位):
var unix = Math.round(+new Date()/1000);
这将为您提供自纪元以来的毫秒数(而不是Unix时间戳):
var milliseconds = new Date().getTime();
var my_timestamp=~~(Date.now()/1000);