UNIX Timestamp 在计算时间或日期上很有用, 在 JavaScript 可以用以下方法取得现在的 UNIX Timestamp.
Date.now() 可以取得现在 UTC timestamp 的 milliseconds (毫秒, 即千分之一秒)。只要将这个数值除 1000, 即可得到 UNIX Timestamp 的格式:
|
1 2 3 4 5 6 7 8 9 10 11 |
<script> // 一些较旧的浏览器不支援 Date.now() if (!Date.now) { Date.now = function() { return new Date().getTime(); } } var d = Math.floor(Date.now() / 1000) // Printing the current timestamp document.write("The current timestamp is: " + d) </script> |