sje*_*397 431
var seconds = new Date().getTime() / 1000;
Run Code Online (Sandbox Code Playgroud)
......自1970年1月1日午夜起,它将为您提供秒数
Kon*_*ert 99
Date.now()
Run Code Online (Sandbox Code Playgroud)
从纪元开始,给出毫秒数.无需使用new.
在这里查看参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
(IE8不支持.)
tfm*_*gue 56
使用new Date().getTime() / 1000是获取秒数的不完整解决方案,因为它生成具有浮点单位的时间戳.
const timestamp = new Date() / 1000; // 1405792936.933
// Technically, .933 would be milliseconds.
Run Code Online (Sandbox Code Playgroud)
更好的解决方案是:
// Rounds the value
const timestamp = Math.round(new Date() / 1000); // 1405792937
// - OR -
// Floors the value
const timestamp = new Date() / 1000 | 0; // 1405792936
Run Code Online (Sandbox Code Playgroud)
没有浮点数的值对于条件语句也更安全,因为浮点数可能会产生不需要的结果.使用float获得的粒度可能超过了需要.
if (1405792936.993 < 1405792937) // true
Run Code Online (Sandbox Code Playgroud)
Nic*_*ver 37
根据你的评论,我认为你正在寻找这样的东西:
var timeout = new Date().getTime() + 15*60*1000; //add 15 minutes;
Run Code Online (Sandbox Code Playgroud)
然后在你的支票中,你正在检查:
if(new Date().getTime() > timeout) {
alert("Session has expired");
}
Run Code Online (Sandbox Code Playgroud)
Laz*_*rus 16
要从Javascript纪元使用获得秒数:
date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;
Run Code Online (Sandbox Code Playgroud)
// The Current Unix Timestamp
// 1443535752 seconds since Jan 01 1970. (UTC)
// Current time in seconds
console.log(Math.floor(new Date().valueOf() / 1000)); // 1443535752
console.log(Math.floor(Date.now() / 1000)); // 1443535752
console.log(Math.floor(new Date().getTime() / 1000)); // 1443535752Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Run Code Online (Sandbox Code Playgroud)
jQuery的
console.log(Math.floor($.now() / 1000)); // 1443535752Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Run Code Online (Sandbox Code Playgroud)
我用这个:
Math.round(Date.now() / 1000)
Run Code Online (Sandbox Code Playgroud)
无需创建新对象(参见文档 Date.now())
这些 JavaScript 解决方案为您提供自 1970 年 1 月 1 日午夜以来的毫秒数或秒数。
IE 9+ 解决方案(IE 8 或更旧版本不支持此功能。):
var timestampInMilliseconds = Date.now();
var timestampInSeconds = Date.now() / 1000; // A float value; not an integer.
timestampInSeconds = Math.floor(Date.now() / 1000); // Floor it to get the seconds.
timestampInSeconds = Date.now() / 1000 | 0; // Also you can do floor it like this.
timestampInSeconds = Math.round(Date.now() / 1000); // Round it to get the seconds.
Run Code Online (Sandbox Code Playgroud)
获取更多信息Date.now():https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
通用解决方案:
// ‘+’ operator makes the operand numeric.
// And ‘new’ operator can be used without the arguments ‘(……)’.
var timestampInMilliseconds = +new Date;
var timestampInSeconds = +new Date / 1000; // A float value; not an intger.
timestampInSeconds = Math.floor(+new Date / 1000); // Floor it to get the seconds.
timestampInSeconds = +new Date / 1000 | 0; // Also you can do floor it like this.
timestampInSeconds = Math.round(+new Date / 1000); // Round it to get the seconds.
Run Code Online (Sandbox Code Playgroud)
小心使用,如果你不想要这种情况。
if(1000000 < Math.round(1000000.2)) // false.
Run Code Online (Sandbox Code Playgroud)
无需初始化变量来包含 Date 对象,因为它Date.now()是静态方法,这意味着可以直接从 API 对象的构造函数访问它。
const ms = Date.now()
const sec = Math.round(ms/1000)
document.write(`seconds: ${sec}`)Run Code Online (Sandbox Code Playgroud)
自 1970 年 1 月 1 日 00:00:00 UTC 以来的秒数实时更新
const element = document.getElementById('root')
setInterval(() => {
let seconds = Math.round(Date.now()/1000)
element.innerHTML = seconds
},1000)Run Code Online (Sandbox Code Playgroud)
Seconds since January 1, 1970 00:00:00 UTC
<h1 id='root'></h1>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
418465 次 |
| 最近记录: |