← 全部文章

當前 Unix 時間戳:紀元時鐘、UTC 時間與即時 Unix 時間

當前 Unix 時間戳是此刻自 1970-01-01 00:00:00 UTC 以來的秒數。了解紀元時鐘如何運作、為什麼 UTC 是基準,以及如何在常見語言中取得當前時間戳。

目前 Unix 時間戳的含義

目前 Unix 時間戳是自 1970 年 1 月 1 日 00:00:00 UTC 起經過的整秒數。即時 epoch 時鐘每秒遞增一次。如果時鐘顯示毫秒,則每秒遞增多次,通常是 13 位的值。

為什麼以 UTC 為基準

Unix 時間以 UTC 為基礎,使不同時區的電腦能就同一時刻達成一個數值上的一致。本地時間只是顯示層。兩個使用者在同一時刻載入頁面時,即使一個看到早晨、另一個看到傍晚,他們也共享相同的目前 Unix 時間戳。

如何取得目前 Unix 時間戳

每種常見語言都有取得目前 Unix 時間的簡短方法。

  • JavaScript 毫秒:Date.now()
  • JavaScript 秒:Math.floor(Date.now() / 1000)
  • Linux 秒:date +%s
  • Python 秒:int(time.time())
  • PHP 秒:time()
  • Go 秒:time.Now().Unix()
  • PostgreSQL 秒:EXTRACT(EPOCH FROM now())

目前時間戳 vs 格式化時間

時間戳適合排序、比較、儲存與 API 負載。格式化日期適合人閱讀。大多數應用儲存時間戳或 UTC 日期時間,只有在向使用者顯示時才進行格式化。

  • 在資料庫與日誌中儲存 UTC 或 Unix 時間戳
  • 以使用者所在時區顯示本地時間
  • 當人需要檢查 API 負載時使用 ISO 8601
  • 當系統需要精簡的數值時使用 Unix 秒或毫秒

FAQ

Does the current Unix timestamp depend on my timezone?
No. The timestamp is based on UTC and is the same worldwide for the same instant.
Why is my current timestamp 13 digits?
A 13-digit current timestamp is usually milliseconds. Divide by 1000 to get Unix seconds.
Where can I see a live Unix epoch clock?
The converter homepage shows a live epoch clock and lets you convert the value to UTC or local time.
Is the current Unix timestamp the same in every programming language?
Yes for the instant, but not always for the unit. JavaScript's Date.now() and Java's System.currentTimeMillis() return milliseconds, while Python time.time(), PHP time(), Go Unix(), and Linux date +%s return seconds. Confirm the unit before comparing or storing values.