我想要一个表示当前日期和时间的数字,比如Unix时间戳。
当前回答
这一个有一个解决方案:在js中将unixtime stamp转换为tim
var a = new Date(UNIX_timestamp*1000);
var hour = a.getUTCHours();
var min = a.getUTCMinutes();
var sec = a.getUTCSeconds();
其他回答
var time = Date.now || function() {
return +new Date;
};
time();
//如果你需要10位数alert('timestamp'+ts());函数ts(){return parseInt(Date.now()/1000);}
您只能使用
var timestamp=new Date().getTime();console.log(时间戳);
以获取当前时间戳。不需要做任何额外的事情。
下面是一个生成时间戳的简单函数,格式为: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())));
}
/**
* Equivalent to PHP's time(), which returns
* current Unix timestamp.
*
* @param {string} unit - Unit of time to return.
* - Use 's' for seconds and 'ms' for milliseconds.
* @return {number}
*/
time(unit = 's') {
return unit == 's' ? Math.floor(Date.now() / 1000) : Date.now()
}