获取当前日期/时间(秒)

时间:2022-10-28 22:46:24

How do I get the current date/time in seconds in Javascript?

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

9 个解决方案

#1


354  

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

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

....能给你从1970年1月1日午夜起的秒吗

Reference

参考

#2


84  

 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

查看这里的引用:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

(Not supported in IE8.)

(不支持IE8)。

#3


38  

Using new Date().getTime() / 1000 is an incomplete solution for obtaining the seconds, because it produces timestamps with floating-point units.

使用new Date(). gettime() / 1000是获取秒的不完整解决方案,因为它生成带有浮点单元的时间戳。

var timestamp = new Date() / 1000; // 1405792936.933
// Technically, .933 would be milliseconds. 

A better solution would be:

一个更好的解决方案是:

var timestamp = new Date() / 1000 | 0; // 1405792936
// Floors the value

// - OR -

var timestamp = Math.round(new Date() / 1000); // 1405792937
// Rounds the value

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

#4


37  

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");
}

#5


14  

To get the number of seconds from the Javascript epoch use:

要获取Javascript历元使用的秒数:

date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;

#6


5  

// 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>

#7


1  

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

要获得关于data .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.

#8


1  

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.

但是要小心这种方法,因为阅读和理解它可能会很困难。

#9


0  

Let me suggest better short cut:

让我提出更好的捷径:

+new Date # Milliseconds since Linux epoch
+new Date / 1000 # Seconds since Linux epoch
Math.round(+new Date / 1000) #Seconds without decimals since Linux epoch

#1


354  

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

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

....能给你从1970年1月1日午夜起的秒吗

Reference

参考

#2


84  

 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

查看这里的引用:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now

(Not supported in IE8.)

(不支持IE8)。

#3


38  

Using new Date().getTime() / 1000 is an incomplete solution for obtaining the seconds, because it produces timestamps with floating-point units.

使用new Date(). gettime() / 1000是获取秒的不完整解决方案,因为它生成带有浮点单元的时间戳。

var timestamp = new Date() / 1000; // 1405792936.933
// Technically, .933 would be milliseconds. 

A better solution would be:

一个更好的解决方案是:

var timestamp = new Date() / 1000 | 0; // 1405792936
// Floors the value

// - OR -

var timestamp = Math.round(new Date() / 1000); // 1405792937
// Rounds the value

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

#4


37  

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");
}

#5


14  

To get the number of seconds from the Javascript epoch use:

要获取Javascript历元使用的秒数:

date = new Date();
milliseconds = date.getTime();
seconds = milliseconds / 1000;

#6


5  

// 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>

#7


1  

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

要获得关于data .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.

#8


1  

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.

但是要小心这种方法,因为阅读和理解它可能会很困难。

#9


0  

Let me suggest better short cut:

让我提出更好的捷径:

+new Date # Milliseconds since Linux epoch
+new Date / 1000 # Seconds since Linux epoch
Math.round(+new Date / 1000) #Seconds without decimals since Linux epoch