我想要一个表示当前日期和时间的数字,比如Unix时间戳。


当前回答

jQuery提供了自己的方法来获取时间戳:

var timestamp = $.now();

(此外,它还实现了(newDate).getTime()表达式)

裁判:http://api.jquery.com/jQuery.now/

其他回答

JavaScript的工作时间是从纪元开始的毫秒数,而大多数其他语言的工作时间都是秒。您可以使用毫秒来工作,但只要您传递一个值来表示PHP,PHP本机函数可能就会失败。所以,为了确保我总是使用秒,而不是毫秒。

这将为您提供Unix时间戳(以秒为单位):

var unix = Math.round(+new Date()/1000);

这将为您提供自纪元以来的毫秒数(而不是Unix时间戳):

var milliseconds = new Date().getTime();

//当前Unix时间戳//自1970年1月1日起,1443534720秒。(UTC)//秒console.log(数学地板(newDate().valueOf()/1000));//1443534720console.log(数学地板(Date.now()/1000));//1443534720console.log(数学地板(newDate().getTime()/1000));//1443534720//毫秒console.log(数学地板(newDate().valueOf()));//1443534720087console.log(数学地板(Date.now()));//1443534720087console.log(数学地板(newDate().getTime()));//1443534720087//jQuery//秒console.log(数学地板($.now()/1000));//1443534720//毫秒console.log($.now());//1443534720087<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js“></script>

您只能使用

var timestamp=new Date().getTime();console.log(时间戳);

以获取当前时间戳。不需要做任何额外的事情。

var my_timestamp=~~(Date.now()/1000);

如果用于日志记录,则可以使用ISOString

new Date().toISOString()

“2019-05-18T20:02:36.694Z”