我想要一个表示当前日期和时间的数字,比如Unix时间戳。
当前回答
我还没见过
Math.floor(Date.now() / 1000); // current time in seconds
另一个我还没看到的是
var _ = require('lodash'); // from here https://lodash.com/docs#now
_.now();
其他回答
更简单的方法:
var timeStamp=event.timestamp || new Date().getTime();
var timestamp = Number(new Date()); // current time as number
var time = Date.now || function() {
return +new Date;
};
time();
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())));
}
前几天,我从JQueryCookie的源代码中学习了一种将给定的Date对象转换为Unix时间戳的非常酷的方法。
下面是一个示例:
var date = new Date();
var timestamp = +date;