【发布时间】:2011-04-19 07:37:32
【问题描述】:
如何在 Javascript 中以秒为单位获取当前日期/时间?
【问题讨论】:
-
请注意,接受的答案将产生一个浮点数。
标签: javascript datetime
如何在 Javascript 中以秒为单位获取当前日期/时间?
【问题讨论】:
标签: javascript datetime
【讨论】:
Date.now()
给出自纪元以来的毫秒数。无需使用new。
在此处查看参考:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
(IE8 不支持。)
【讨论】:
Date.now() / 1000 自己回答问题
Date.now() / 1000 也没有回答这个问题,因为它给出了以毫秒为单位的部分分数的值
使用new Date().getTime() / 1000 是获取秒数的不完整解决方案,因为它会生成带有浮点单位的时间戳。
new Date() / 1000; // 1405792936.933
// Technically, .933 would be in milliseconds
改为使用:
Math.round(Date.now() / 1000); // 1405792937
// Or
Math.floor(Date.now() / 1000); // 1405792936
// Or
Math.ceil(Date.now() / 1000); // 1405792937
// Note: In general, I recommend `Math.round()`,
// but there are use cases where
// `Math.floor()` and `Math.ceil()`
// might be better suited.
此外,没有浮点数的值对于条件语句更安全,因为使用浮点数获得的粒度可能会导致不需要的结果。例如:
if (1405792936.993 < 1405792937) // true
警告:位运算符在用于处理时间戳时可能会导致问题。例如,(new Date() / 1000) | 0 也可用于将值“下限”为秒,但该代码会导致以下问题:
【讨论】:
Math.round(new Date() / 1000)
| 0
Math.round 而不是Math.floor/Math.trunc?
Math.round() 是最好的。但是,是的,我同意有些用例不是最好的,Math.floor() 和 Math.ceil() 更合适。我已经更新了我的答案以说明相同的内容。
根据您的评论,我认为您正在寻找这样的东西:
var timeout = new Date().getTime() + 15*60*1000; //add 15 minutes;
那么在你的检查中,你正在检查:
if(new Date().getTime() > timeout) {
alert("Session has expired");
}
【讨论】:
要获取 Javascript 纪元的秒数,请使用:
date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;
【讨论】:
// 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)); // 1443535752
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
jQuery
console.log(Math.floor($.now() / 1000)); // 1443535752
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
【讨论】:
【讨论】:
这些 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.
获取更多关于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.
如果您不想要这种情况,请小心使用。
if(1000000 < Math.round(1000000.2)) // false.
【讨论】:
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
除了使用按位运算符来确定天数之外,其他与之前相同。
【讨论】:
自 1970 年 1 月 1 日以来,您可以找到另一种以秒/毫秒为单位获取时间的方法:
var milliseconds = +new Date;
var seconds = milliseconds / 1000;
但要小心这种方法,因为它可能难以阅读和理解。
【讨论】:
更好的捷径:
+new Date # Milliseconds since Linux epoch
+new Date / 1000 # Seconds since Linux epoch
Math.round(+new Date / 1000) #Seconds without decimals since Linux epoch
【讨论】:
在 2020 年的某一天,在 Chrome 80.0.3987.132 中,这将提供 1584533105
~~(new Date()/1000) // 1584533105
Number.isInteger(~~(new Date()/1000)) // true
【讨论】:
获取今天的总秒数:
getTodaysTotalSeconds(){
let date = new Date();
return +(date.getHours() * 60 * 60) + (date.getMinutes() * 60) + date.getSeconds();
}
我添加了+ 作为回报,返回int。这可能对其他开发人员有所帮助。 :)
【讨论】:
如果您只需要在三个 JS 中使用几秒钟,请在函数中使用以下代码之一使用 window.requestAnimationFrame()
let sec = parseInt(Date.now().toString()[10]); console.log(' counting Seconds => '+ sec );
或
let currentTime= Date.now();
let secAsString= time.toString()[10];
let sec = parseInt(t);
console.log('counting Seconds =>'+ sec );
【讨论】: