我想要一个表示当前日期和时间的数字,比如Unix时间戳。
当前回答
console.log(newDate().valueOf());//返回自epoch以来的毫秒数
其他回答
更简单的方法:
var timeStamp=event.timestamp || new Date().getTime();
我还没见过
Math.floor(Date.now() / 1000); // current time in seconds
另一个我还没看到的是
var _ = require('lodash'); // from here https://lodash.com/docs#now
_.now();
我喜欢这个,因为它很小:
+new Date
我也喜欢这个,因为它同样短,而且与现代浏览器兼容,超过500人投票认为它更好:
Date.now()
var timestamp = Number(new Date()); // current time as number
Moment.js可以消除处理Javascript Dates时的许多痛苦。
参见:http://momentjs.com/docs/#/displaying/unix-时间戳/
moment().unix();