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