我如何获得当前日期或/和时间在秒使用Javascript?


当前回答

//当前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");
}

要获得今天的总秒数:

getTodaysTotalSeconds(){
    let date = new Date();        
    return +(date.getHours() * 60 * 60) + (date.getMinutes() * 60) + date.getSeconds();
}

我在return中使用了add +它在int中返回。这可能对其他开发人员有所帮助。:)

一种简单快速的方法,不需要创建日期对象并给出一个整数作为结果(如果发送小数,接受秒的api将会出错)

var nowincos = ~~(日期。 游戏机。log (nowInSeconds);

var seconds = new Date().getTime() / 1000;

....能告诉你从1970年1月1日午夜开始的秒数吗

参考

从1970年1月1日开始,你还可以用秒/毫秒来表示时间:

var milliseconds = +new Date;        
var seconds = milliseconds / 1000;

但是要小心使用这种方法,因为阅读和理解它可能很棘手。