以秒为单位获取当前日期/时间

Hai*_*ood 282 javascript datetime

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

sje*_*397 431

var seconds = new Date().getTime() / 1000;
Run Code Online (Sandbox Code Playgroud)

......自1970年1月1日午夜起,它将为您提供秒数

参考

  • 值得注意的是,你想在Math.floor()中包装它,否则你得到一个小数. (19认同)
  • 虽然这是准确的,但什么时候有用?:) (11认同)
  • 这将是有用的一种方法是计算两次之间经过的时间. (10认同)
  • @Nick - 我认为所有的例子都必然是推测性的,并且听起来很人为 - 但我会试一试:)假设你的数据时间标记为unix时间,并想确定它的年龄.更多但我认为这是'当前日期/时间以秒为单位'最有可能的意思; 只是我的直觉. (6认同)
  • 我需要这个的应用程序是使用PHP后端,因为time()函数已经返回了这个.它也适用于时间戳,因为您可以轻松获得time()作为当前时间与上一次存储在数据库中的时间戳之间的差异,即用户发布内容时的时间差.如果您希望获得格式化的时间,例如"2015年10月22日",您可以制作自己的函数,将时间戳作为参数返回,或者使用Stack中已有的函数.无论如何,希望能让您了解何时有用.@NickCraver (3认同)
  • 它几乎可用于任何经过时间的测量,其中第二粒度不会溢出.所以喜欢,在很多地方. (3认同)
  • 如果您要将时间戳发送到期望时间戳以秒而不是毫秒为单位的 PHP 函数,则很有用。 (2认同)

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不支持.)

  • 这个毫秒的答案甚至没有回答所提出的问题。“如何获取当前日期/时间(以秒为单位)” (9认同)
  • `Date.now() / 1000` 来回答问题本身 (6认同)
  • @magician11 - `Date.now() / 1000` 也没有回答这个问题,因为它给出了带有部分分数的值(以毫秒为单位) (5认同)
  • Math.floor(Date.now()/1000) (4认同)

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)

  • 完全同意使用`Math.round(new Date() / 1000)` (3认同)

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)


blu*_*xff 8

// 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
Run 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));               // 1443535752
Run 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)


V. *_*bor 7

我用这个:

Math.round(Date.now() / 1000)
Run Code Online (Sandbox Code Playgroud)

无需创建新对象(参见文档 Date.now()


Кон*_*Ван 5

这些 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)


Gas*_*ass 5

无需初始化变量来包含 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)