我想要一个表示当前日期和时间的数字,比如Unix时间戳。


当前回答

var d = new Date();
console.log(d.valueOf()); 

其他回答

time = Math.round(((new Date()).getTime()-Date.UTC(1970,0,1))/1000);

除了其他选项,如果您想要日期格式ISO,可以直接获取

console.log(newDate().toISOString());

前几天,我从JQueryCookie的源代码中学习了一种将给定的Date对象转换为Unix时间戳的非常酷的方法。

下面是一个示例:

var date = new Date();
var timestamp = +date;

console.log(newDate().valueOf());//返回自epoch以来的毫秒数

Moment.js可以消除处理Javascript Dates时的许多痛苦。

参见:http://momentjs.com/docs/#/displaying/unix-时间戳/

moment().unix();