Javascript 以秒为单位获取当前日期/时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3830244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current date/time in seconds
提问by Hailwood
How do I get the current date/time in seconds in Javascript?
如何在 Javascript 中以秒为单位获取当前日期/时间?
回答by sje397
回答by Konstantin Schubert
Date.now()
gives milliseconds since epoch. No need to use new
.
给出自纪元以来的毫秒数。无需使用new
.
Check out the reference here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
在此处查看参考:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
(Not supported in IE8.)
(在 IE8 中不支持。)
回答by tfmontague
Using new Date().getTime() / 1000
is an incomplete solution for obtaining the seconds, because it produces timestamps with floating-point units.
Usingnew Date().getTime() / 1000
是获取秒数的不完整解决方案,因为它生成带有浮点单位的时间戳。
const timestamp = new Date() / 1000; // 1405792936.933
// Technically, .933 would be milliseconds.
A better solution would be:
更好的解决方案是:
// Rounds the value
const timestamp = Math.round(new Date() / 1000); // 1405792937
// - OR -
// Floors the value
const timestamp = new Date() / 1000 | 0; // 1405792936
Values without floats are also safer for conditional statements, as the float may produce unwanted results. The granularity you obtain with a float may be more than needed.
没有浮点数的值对于条件语句也更安全,因为浮点数可能会产生不需要的结果。您使用浮点数获得的粒度可能超出需要。
if (1405792936.993 < 1405792937) // true
回答by Nick Craver
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");
}
回答by Lazarus
To get the number of seconds from the Javascript epoch use:
要从 Javascript 纪元中获取秒数,请使用:
date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;
回答by blueberry0xff
// 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
jQuery
console.log(Math.floor($.now() / 1000)); // 1443535752
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
回答by Константин Ван
These JavaScript solutions give you the milliseconds or the seconds since the midnight, January 1st, 1970.
这些 JavaScript 解决方案为您提供自 1970 年 1 月 1 日午夜以来的毫秒数或秒数。
The IE 9+ solution(IE 8 or the older version doesn't support this.):
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.
To get more information about Date.now()
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
获取更多信息Date.now()
:https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now
The generic solution:
通用解决方案:
// ‘+' 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.
Be careful to use, if you don't want something like this case.
小心使用,如果你不想要这种情况。
if(1000000 < Math.round(1000000.2)) // false.
回答by CEO RecDecLec
Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000
This should give you the milliseconds from the beginning of the day.
这应该给你从一天开始的毫秒数。
(Date.now()-Math.floor(Date.now()/1000/60/60/24)*24*60*60*1000)/1000
This should give you seconds.
这应该给你几秒钟。
(Date.now()-(Date.now()/1000/60/60/24|0)*24*60*60*1000)/1000
Same as previous except uses a bitwise operator to floor the amount of days.
除了使用按位运算符来计算天数外,与之前相同。
回答by tetta
You can met another way to get time in seconds/milliseconds since 1 Jan 1970:
自 1970 年 1 月 1 日以来,您可以遇到另一种以秒/毫秒为单位获取时间的方法:
var milliseconds = +new Date;
var seconds = milliseconds / 1000;
But be careful with such approach, cause it might be tricky to read and understand it.
但是要小心这种方法,因为阅读和理解它可能会很棘手。
回答by dr.dimitru
Better short cuts:
更好的捷径:
+new Date # Milliseconds since Linux epoch
+new Date / 1000 # Seconds since Linux epoch
Math.round(+new Date / 1000) #Seconds without decimals since Linux epoch