Get current date/time in seconds

如何在Javascript中以秒为单位获取当前日期/时间?


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

....will give you the seconds since midnight, 1 Jan 1970

Reference


 Date.now()

gives milliseconds since epoch. No need to use new .

Check out the reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

(Not supported in IE8.)


Based on your comment, I think you're looking for something like this:

var timeout = new Date().getTime() + 15*60*1000; //add 15 minutes;

Then in your check, you're checking:

if(new Date().getTime() > timeout) {
  alert("Session has expired");
}
链接地址: http://www.djcxy.com/p/13576.html

上一篇: '+新日期'中的加号是做什么的

下一篇: 以秒为单位获取当前日期/时间