如何在我的代码中获取时间戳

How can I get timeStamp in my code

我想在页面加载时获取时间戳,但我不能。

这是我的代码:

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p id = "x">time</p>
        <script src="jquery/jquery1.12.js"></script>
        <script>
            $(document).ready(function(event){
               $("#x").text(event.timeStamp);
            })
        </script>
    </body>
</html>

您可以使用 Date 对象来完成这项工作。 now() 方法 return 以毫秒为单位的时间戳。当然 javascript return 带有客户端时间的时间戳。

$(document).ready(function(event){
    var timeStamp = Math.floor(Date.now() / 1000);
    $("div").text(timeStamp);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div></div>

您不会在就绪调用中获得事件对象。作为解决方法,您可以使用 $.now() 例如:

$(document).ready(function(){
  $("#x").text($.now());
});