On Unix, I can run date '+%s'
to get the amount of seconds since epoch. But I need to query that in a browser front-end, not back-end.
在Unix上,我可以运行date '+%s'来获得从epoch起的秒数。但是我需要在浏览器前端而不是后端进行查询。
Is there a way to find out seconds since Epoch in JavaScript?
是否有一种方法可以在JavaScript内找到时间?
7 个解决方案
#1
169
var seconds = new Date() / 1000;
Or, for a less hacky version:
或者,为了一个不那么陈腐的版本:
var d = new Date();
var seconds = d.getTime() / 1000;
Don't forget to Math.floor()
or Math.round()
to round to nearest whole number or you might get a very odd decimal that you don't want:
不要忘记用Math.floor()或Math.round()来表示最接近的整数,否则你可能会得到一个你不想要的非常奇怪的小数:
var d = new Date();
var seconds = Math.round(d.getTime() / 1000);
#2
48
Try this:
试试这个:
new Date().getTime() / 1000
You might want to use Math.floor()
or Math.round()
to cut milliseconds fraction.
您可能需要使用Math.floor()或Math.round()来减少毫秒数。
#3
30
You wanted seconds since epoch
你想要几秒钟的时间
function seconds_since_epoch(){ return Math.floor( Date.now() / 1000 ) }
example use
示例使用
foo = seconds_since_epoch();
#4
8
The above solutions use instance properties. Another way is to use the class property Date.now
:
上述解决方案使用实例属性。另一种方法是使用类属性数据。
var time_in_millis = Date.now();
var time_in_seconds = time_in_millis / 1000;
If you want time_in_seconds to be an integer you have 2 options:
如果您希望time_in_seconds是整数,您有两个选项:
a. If you want to be consistent with C style truncation:
a.如果您希望与C风格的截断保持一致:
time_in_seconds_int = time_in_seconds >= 0 ?
Math.floor(time_in_seconds) : Math.ceil(time_in_seconds);
b. If you want to just have the mathematical definition of integer division to hold, just take the floor. (Python's integer division does this).
b.如果你想要有整数除法的数学定义,只要在地板上。(Python的整数除法就是这么做的)。
time_in_seconds_int = Math.floor(time_in_seconds);
#5
7
If you want only seconds as a whole number without the decimals representing milliseconds still attached, use this:
如果你只需要数秒作为一个整数,而没有代表毫秒的小数,可以用这个:
var seconds = Math.floor(new Date() / 1000);
#6
5
You can create a Date object (which will have the current time in it) and then call getTime()
to get the ms since epoch.
您可以创建一个Date对象(其中将包含当前时间),然后调用getTime()来获取自纪元以来的ms。
var ms = new Date().getTime();
If you want seconds, then divide it by 1000:
如果你想要秒,那么除以1000:
var sec = new Date().getTime() / 1000;
#7
4
My preferred way:
我的首选方法:
var msEpoch = (+new Date());
var sEpoch = (+new Date()) / 1000;
For more information on the +
jump down the rabbit hole.
要了解更多关于+跳越兔子洞的信息。
#1
169
var seconds = new Date() / 1000;
Or, for a less hacky version:
或者,为了一个不那么陈腐的版本:
var d = new Date();
var seconds = d.getTime() / 1000;
Don't forget to Math.floor()
or Math.round()
to round to nearest whole number or you might get a very odd decimal that you don't want:
不要忘记用Math.floor()或Math.round()来表示最接近的整数,否则你可能会得到一个你不想要的非常奇怪的小数:
var d = new Date();
var seconds = Math.round(d.getTime() / 1000);
#2
48
Try this:
试试这个:
new Date().getTime() / 1000
You might want to use Math.floor()
or Math.round()
to cut milliseconds fraction.
您可能需要使用Math.floor()或Math.round()来减少毫秒数。
#3
30
You wanted seconds since epoch
你想要几秒钟的时间
function seconds_since_epoch(){ return Math.floor( Date.now() / 1000 ) }
example use
示例使用
foo = seconds_since_epoch();
#4
8
The above solutions use instance properties. Another way is to use the class property Date.now
:
上述解决方案使用实例属性。另一种方法是使用类属性数据。
var time_in_millis = Date.now();
var time_in_seconds = time_in_millis / 1000;
If you want time_in_seconds to be an integer you have 2 options:
如果您希望time_in_seconds是整数,您有两个选项:
a. If you want to be consistent with C style truncation:
a.如果您希望与C风格的截断保持一致:
time_in_seconds_int = time_in_seconds >= 0 ?
Math.floor(time_in_seconds) : Math.ceil(time_in_seconds);
b. If you want to just have the mathematical definition of integer division to hold, just take the floor. (Python's integer division does this).
b.如果你想要有整数除法的数学定义,只要在地板上。(Python的整数除法就是这么做的)。
time_in_seconds_int = Math.floor(time_in_seconds);
#5
7
If you want only seconds as a whole number without the decimals representing milliseconds still attached, use this:
如果你只需要数秒作为一个整数,而没有代表毫秒的小数,可以用这个:
var seconds = Math.floor(new Date() / 1000);
#6
5
You can create a Date object (which will have the current time in it) and then call getTime()
to get the ms since epoch.
您可以创建一个Date对象(其中将包含当前时间),然后调用getTime()来获取自纪元以来的ms。
var ms = new Date().getTime();
If you want seconds, then divide it by 1000:
如果你想要秒,那么除以1000:
var sec = new Date().getTime() / 1000;
#7
4
My preferred way:
我的首选方法:
var msEpoch = (+new Date());
var sEpoch = (+new Date()) / 1000;
For more information on the +
jump down the rabbit hole.
要了解更多关于+跳越兔子洞的信息。