我如何获得当前日期或/和时间在秒使用Javascript?
当前回答
在2020年的某一天,在Chrome 80.0.3987.132中,这给出了1584533105
~~(new Date()/1000) // 1584533105
Number.isInteger(~~(new Date()/1000)) // true
其他回答
//当前Unix时间戳 // 1443535752秒自1970年1月1日。(UTC) //当前时间,单位为秒 console.log(数学。floor(new Date().valueOf() / 1000));/ / 1443535752 console.log(Math.floor(Date.now() / 1000));/ / 1443535752 console.log(数学。floor(new Date().getTime() / 1000));/ / 1443535752 < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本>
jQuery
console.log(Math.floor($.now() / 1000));/ / 1443535752 < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本>
根据你的评论,我认为你在寻找这样的东西:
var timeout = new Date().getTime() + 15*60*1000; //add 15 minutes;
然后在你的检查中,你在检查:
if(new Date().getTime() > timeout) {
alert("Session has expired");
}
在2020年的某一天,在Chrome 80.0.3987.132中,这给出了1584533105
~~(new Date()/1000) // 1584533105
Number.isInteger(~~(new Date()/1000)) // true
Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000
这应该会给出从一天开始算起的毫秒数。
(Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000)/1000
这应该会给你一些时间。
(Date.now()-(Date.now()/1000/60/60/24|0)*24*60*60*1000)/1000
与前面相同,只是使用了位运算符来计算天数的下限。
var seconds = new Date().getTime() / 1000;
....能告诉你从1970年1月1日午夜开始的秒数吗
参考