🌟 工具介紹🌟 Tool Introduction
時間戳(Timestamp)是程式開發中不可或缺的概念,它提供了一種統一、無歧義的方式來表示時間點。無論你是前端開發者需要處理 API 回應、後端工程師需要記錄日誌時間,還是數據分析師需要處理時序資料,時間戳轉換器都是必備工具。Timestamps are an essential concept in software development, providing a unified and unambiguous way to represent points in time. Whether you're a frontend developer handling API responses, a backend engineer recording log times, or a data analyst processing time-series data, a timestamp converter is an indispensable tool.
💡 為什麼需要時間戳轉換器?💡 Why Do You Need a Timestamp Converter?
- 統一標準Unified Standard:不同系統使用不同的時間表示方式,時間戳提供統一標準: Different systems use different time representations; timestamps provide a unified standard
- 避免時區混淆Avoid Timezone Confusion:時間戳基於 UTC,消除時區差異造成的問題: Timestamps are based on UTC, eliminating issues caused by timezone differences
- 簡化計算Simplified Calculations:以數字表示時間,方便進行時間差計算和排序: Representing time as numbers makes time difference calculations and sorting easier
- 資料交換Data Exchange:API、資料庫、日誌系統的時間資料交換標準: Standard for time data exchange in APIs, databases, and logging systems
- 跨平台兼容Cross-platform Compatibility:所有程式語言和平台都支援時間戳: All programming languages and platforms support timestamps
🔄
多格式轉換Multi-format Conversion
支援 Unix 時間戳、ISO 8601、RFC 3339、本地時間等多種格式互轉Supports conversion between Unix timestamps, ISO 8601, RFC 3339, local time, and more
🌍
時區處理Timezone Handling
自動偵測和轉換不同時區,支援 UTC、本地時區、自訂時區Auto-detect and convert between timezones; supports UTC, local timezone, and custom timezones
⏱️
即時轉換Instant Conversion
輸入即時轉換,無需等待,支援秒級和毫秒級精度Real-time conversion as you type; supports second and millisecond precision
📋
一鍵複製One-click Copy
轉換結果一鍵複製到剪貼簿,方便快速使用Copy conversion results to clipboard with one click for quick use
💻
程式碼範例Code Examples
提供主流程式語言的時間戳處理範例代碼Provides timestamp handling examples for major programming languages
🔒
100% 本地處理100% Local Processing
所有轉換在瀏覽器本地完成,資料不上傳伺服器,保護隱私All conversions are done locally in your browser; data never leaves your device
🚀 快速開始指南🚀 Quick Start Guide
2
選擇轉換方向Select Conversion Direction
選擇「時間戳 → 日期時間」或「日期時間 → 時間戳」Choose "Timestamp → Date/Time" or "Date/Time → Timestamp"
3
輸入數據Enter Data
輸入時間戳數字(如:1706342400)或選擇日期時間Enter a timestamp number (e.g., 1706342400) or select a date/time
4
選擇時區Select Timezone
選擇目標時區(預設為本地時區)或 UTCChoose target timezone (default is local timezone) or UTC
5
獲取結果Get Results
查看轉換結果,點擊複製按鈕即可使用View the conversion result and click the copy button to use it
💡 快速提示💡 Quick Tips
- 當前時間戳Current Timestamp:點擊「使用當前時間」按鈕快速獲取現在的時間戳: Click "Use Current Time" to quickly get the current timestamp
- 格式選擇Format Selection:支援多種輸出格式(ISO 8601、RFC 3339、本地格式): Supports multiple output formats (ISO 8601, RFC 3339, local format)
- 精度選擇Precision Selection:可選擇秒級時間戳(10位)或毫秒級(13位): Choose second-precision (10 digits) or millisecond-precision (13 digits) timestamps
📚 詳細使用教學📚 Detailed Tutorial
什麼是時間戳?What is a Timestamp?
時間戳是從某個特定時間點(稱為「紀元」)開始計算的秒數或毫秒數。最常用的是 Unix 時間戳,它從 1970 年 1 月 1 日 00:00:00 UTC 開始計算。A timestamp is the number of seconds or milliseconds counted from a specific point in time called the "epoch". The most common is the Unix timestamp, which counts from January 1, 1970 00:00:00 UTC.
🕐 Unix 時間戳🕐 Unix Timestamp
定義Definition:自 1970-01-01 00:00:00 UTC 以來的秒數: The number of seconds since 1970-01-01 00:00:00 UTC
- 格式Format:10 位整數(秒級)或 13 位整數(毫秒級): 10-digit integer (seconds) or 13-digit integer (milliseconds)
- 範例Examples::
- 1706342400 = 2024-01-27 08:00:00 UTC(秒級)1706342400 = 2024-01-27 08:00:00 UTC (seconds)
- 1706342400000 = 2024-01-27 08:00:00.000 UTC(毫秒級)1706342400000 = 2024-01-27 08:00:00.000 UTC (milliseconds)
- 優點Advantages:簡單、高效、易於計算時間差: Simple, efficient, easy to calculate time differences
- 缺點Disadvantages:不直觀,需要轉換才能閱讀: Not human-readable, requires conversion
常見時間格式Common Time Formats
時區轉換Timezone Conversion
🌍 理解時區🌍 Understanding Timezones
時區是地球上不同區域使用的標準時間。全球分為 24 個主要時區,以 UTC(協調世界時)為基準。Timezones are the standard times used in different regions of the world. The globe is divided into 24 major timezones, based on UTC (Coordinated Universal Time).
常見時區範例Common Timezone Examples
- UTC(協調世界時)UTC (Coordinated Universal Time):標準時間,無偏移量(+00:00): Standard time, no offset (+00:00)
- GMT(格林威治標準時間)GMT (Greenwich Mean Time):與 UTC 基本相同: Essentially the same as UTC
- CST(中國標準時間)CST (China Standard Time):UTC+8,比 UTC 快 8 小時: UTC+8, 8 hours ahead of UTC
- EST(美東標準時間)EST (Eastern Standard Time):UTC-5,比 UTC 慢 5 小時: UTC-5, 5 hours behind UTC
- JST(日本標準時間)JST (Japan Standard Time):UTC+9,比 UTC 快 9 小時: UTC+9, 9 hours ahead of UTC
🎯 時區轉換範例🎯 Timezone Conversion Example
情境Scenario:Unix 時間戳 1706342400 在不同時區的表示: Unix timestamp 1706342400 displayed in different timezones
- UTC:2024-01-27 08:00:00
- 台北 (UTC+8)Taipei (UTC+8):2024-01-27 16:00:00
- 紐約 (UTC-5)New York (UTC-5):2024-01-27 03:00:00
- 倫敦 (UTC+0)London (UTC+0):2024-01-27 08:00:00
- 東京 (UTC+9)Tokyo (UTC+9):2024-01-27 17:00:00
💡 重點:同一個時間戳在不同時區的本地時間不同,但表示的是同一個瞬間💡 Key Point: The same timestamp shows different local times in different timezones, but represents the same instant in time
使用情境Use Cases
1️⃣ API 開發1️⃣ API Development
API 回應中的時間資料通常使用時間戳或 ISO 8601 格式:Time data in API responses typically uses timestamps or ISO 8601 format:
{
"created_at": 1706342400,
"updated_at": "2024-01-27T08:00:00Z",
"user": {
"last_login": 1706342400000
}
}
需要轉換為本地時間顯示給用戶。Needs to be converted to local time for user display.
2️⃣ 日誌分析2️⃣ Log Analysis
伺服器日誌通常記錄時間戳,需要轉換為可讀格式進行分析:Server logs typically record timestamps and need conversion to readable format for analysis:
[1706342400] INFO: User login successful
[1706342450] ERROR: Database connection failed
[1706342500] WARN: High memory usage detected
3️⃣ 資料庫查詢3️⃣ Database Queries
資料庫中的時間欄位可能以時間戳儲存,查詢時需要轉換:Database time fields may be stored as timestamps and need conversion for queries:
-- 查詢 2024 年 1 月 27 日的數據Query data for January 27, 2024
SELECT * FROM orders
WHERE created_at >= 1706284800
AND created_at < 1706371200;
4️⃣ 前端顯示4️⃣ Frontend Display
將後端返回的時間戳轉換為用戶友好的格式:Convert backend timestamps to user-friendly formats:
- 1706342400 → "2024年1月27日 下午4:00"1706342400 → "January 27, 2024 4:00 PM"
- 1706342400 → "2 小時前"1706342400 → "2 hours ago"
- 1706342400 → "昨天 16:00"1706342400 → "Yesterday 4:00 PM"
❓ 常見問題 FAQ❓ Frequently Asked Questions
Q1: 為什麼 Unix 時間戳從 1970 年開始?Q1: Why does the Unix timestamp start from 1970?
A: 1970 年 1 月 1 日 00:00:00 UTC 被稱為「Unix 紀元」(Unix Epoch)。這是 Unix 作業系統最初發佈時選擇的起始點。選擇這個日期的原因包括:January 1, 1970 00:00:00 UTC is called the "Unix Epoch". This was the starting point chosen when the Unix operating system was originally released. Reasons for choosing this date include:
- 1970 年是 Unix 系統開發的早期階段1970 was during the early development stage of the Unix system
- 選擇一個整數起點便於計算Choosing an integer starting point makes calculations easier
- 這個日期已成為業界標準,所有系統都遵循This date has become the industry standard that all systems follow
Q2: 10 位和 13 位時間戳有什麼區別?Q2: What's the difference between 10-digit and 13-digit timestamps?
A:
- 10 位時間戳10-digit timestamp:精確到秒,範例
1706342400: Precise to seconds, e.g., 1706342400
- 13 位時間戳13-digit timestamp:精確到毫秒,範例
1706342400000: Precise to milliseconds, e.g., 1706342400000
JavaScript 的 Date.now() 返回 13 位毫秒時間戳,而大多數後端語言(如 PHP、Python)預設使用 10 位秒時間戳。JavaScript's Date.now() returns a 13-digit millisecond timestamp, while most backend languages (like PHP, Python) default to 10-digit second timestamps.
💡 轉換方法:秒轉毫秒乘以 1000,毫秒轉秒除以 1000💡 Conversion method: Multiply by 1000 for seconds to milliseconds, divide by 1000 for milliseconds to seconds
Q3: ISO 8601 格式中的 'T' 和 'Z' 是什麼意思?Q3: What do 'T' and 'Z' mean in ISO 8601 format?
A:
- T:是日期和時間的分隔符號,表示後面是時間部分: The separator between date and time, indicating the time portion follows
範例:2024-01-27T08:00:00Example: 2024-01-27T08:00:00
- Z:表示 UTC 時區(Zulu time),相當於 +00:00: Indicates UTC timezone (Zulu time), equivalent to +00:00
範例:2024-01-27T08:00:00Z(UTC時間)Example: 2024-01-27T08:00:00Z (UTC time)
如果時間不是 UTC,會顯示時區偏移量:2024-01-27T16:00:00+08:00(台北時間)If not UTC, the timezone offset is shown: 2024-01-27T16:00:00+08:00 (Taipei time)
Q4: 什麼是 Y2K38 問題?Q4: What is the Y2K38 problem?
A: Y2K38 問題(也稱為 Unix 千年蟲)是指使用 32 位元有符號整數儲存時間戳的系統,最大值為 2,147,483,647,對應的日期是 2038 年 1 月 19 日 03:14:07 UTC。超過這個時間後,時間戳會溢位變成負數,導致系統錯誤。The Y2K38 problem (also known as the Unix Millennium Bug) affects systems that store timestamps as 32-bit signed integers, with a maximum value of 2,147,483,647, corresponding to January 19, 2038 03:14:07 UTC. After this time, the timestamp will overflow to a negative number, causing system errors.
解決方案Solutions:
- 使用 64 位元整數儲存時間戳(可支援到數百億年後)Use 64-bit integers to store timestamps (supports billions of years into the future)
- 現代程式語言和作業系統已大多升級為 64 位元Most modern programming languages and operating systems have upgraded to 64-bit
- 仍需注意舊系統和嵌入式設備的相容性Legacy systems and embedded devices may still need compatibility considerations
Q5: 如何處理夏令時間(Daylight Saving Time)?Q5: How do you handle Daylight Saving Time (DST)?
A: 夏令時間是某些地區在夏季將時鐘撥快一小時的做法。使用時間戳的最大優勢之一就是自動處理夏令時間:Daylight Saving Time is the practice in some regions of moving clocks forward by one hour during summer. One of the biggest advantages of using timestamps is automatic DST handling:
- 時間戳基於 UTCTimestamps are based on UTC,不受夏令時間影響, unaffected by DST
- 轉換為本地時間時,系統會自動應用夏令時間規則When converting to local time, the system automatically applies DST rules
- 避免直接操作本地時間,統一使用時間戳或 UTC 時間Avoid manipulating local time directly; use timestamps or UTC time consistently
⚠️ 注意:不同年份的夏令時間規則可能不同,使用專業時間庫(如 Moment.js、date-fns)可確保正確處理⚠️ Note: DST rules may vary by year; using professional time libraries (like Moment.js, date-fns) ensures correct handling
🎯 進階技巧🎯 Advanced Tips
程式開發應用Programming Applications
JavaScript
// 獲取當前時間戳(毫秒)Get current timestamp (milliseconds)
const timestamp = Date.now();
console.log(timestamp); // 1706342400000
// 獲取當前時間戳(秒)Get current timestamp (seconds)
const timestampInSeconds = Math.floor(Date.now() / 1000);
console.log(timestampInSeconds); // 1706342400
// 時間戳轉日期Timestamp to date
const date = new Date(1706342400000);
console.log(date.toISOString()); // "2024-01-27T08:00:00.000Z"
console.log(date.toLocaleString('zh-TW', { timeZone: 'Asia/Taipei' }));
// "2024/1/27 下午4:00:00"
// 日期轉時間戳Date to timestamp
const specificDate = new Date('2024-01-27T16:00:00+08:00');
const specificTimestamp = specificDate.getTime();
console.log(specificTimestamp); // 1706342400000
Python
import time
from datetime import datetime, timezone
# 獲取當前時間戳(秒)Get current timestamp (seconds)
timestamp = int(time.time())
print(timestamp) # 1706342400
# 時間戳轉日期Timestamp to date
dt = datetime.fromtimestamp(1706342400, tz=timezone.utc)
print(dt.isoformat()) # "2024-01-27T08:00:00+00:00"
# 轉換為本地時間Convert to local time
import pytz
taipei_tz = pytz.timezone('Asia/Taipei')
taipei_time = dt.astimezone(taipei_tz)
print(taipei_time.strftime('%Y-%m-%d %H:%M:%S'))
# "2024-01-27 16:00:00"
# 日期轉時間戳Date to timestamp
specific_date = datetime(2024, 1, 27, 16, 0, 0, tzinfo=taipei_tz)
specific_timestamp = int(specific_date.timestamp())
print(specific_timestamp) # 1706342400
PHP
<?php
// 獲取當前時間戳(秒)Get current timestamp (seconds)
$timestamp = time();
echo $timestamp; // 1706342400
// 時間戳轉日期Timestamp to date
$date = date('Y-m-d H:i:s', 1706342400);
echo $date; // "2024-01-27 08:00:00"
// ISO 8601 格式format
$isoDate = date('c', 1706342400);
echo $isoDate; // "2024-01-27T08:00:00+00:00"
// 日期轉時間戳Date to timestamp
$specificTimestamp = strtotime('2024-01-27 16:00:00 +0800');
echo $specificTimestamp; // 1706342400
// 時區轉換Timezone conversion
date_default_timezone_set('Asia/Taipei');
$taipeiTime = date('Y-m-d H:i:s', 1706342400);
echo $taipeiTime; // "2024-01-27 16:00:00"
?>
最佳實踐Best Practices
1️⃣ 統一使用 UTC 儲存1️⃣ Store in UTC Consistently
- 資料庫中的時間欄位統一使用 UTC 時區Use UTC timezone for all time fields in the database
- API 傳輸使用 UTC 時間戳或 ISO 8601 with ZUse UTC timestamps or ISO 8601 with Z for API transmission
- 僅在顯示給用戶時轉換為本地時區Only convert to local timezone when displaying to users
2️⃣ 使用專業時間庫2️⃣ Use Professional Time Libraries
- JavaScript:Moment.js、date-fns、Day.js
- Python:pytz、python-dateutil、Arrow
- PHP:Carbon、Chronos
- 避免手動計算時間,減少錯誤Avoid manual time calculations to reduce errors
3️⃣ 注意精度問題3️⃣ Mind Precision Issues
- JavaScript 使用毫秒,大多數後端使用秒JavaScript uses milliseconds; most backends use seconds
- API 文件應明確說明時間戳精度API documentation should clearly state timestamp precision
- 轉換時注意乘以或除以 1000Remember to multiply or divide by 1000 when converting
4️⃣ 處理邊界情況4️⃣ Handle Edge Cases
- 檢查時間戳是否為負數(1970年之前)Check if timestamp is negative (before 1970)
- 檢查是否超過 Y2K38 限制Check if exceeding Y2K38 limits
- 驗證日期字串格式是否正確Validate date string format correctness
- 處理無效時區輸入Handle invalid timezone inputs
5️⃣ 使用者體驗優化5️⃣ User Experience Optimization
- 顯示相對時間:「2 小時前」、「昨天」Display relative time: "2 hours ago", "yesterday"
- 根據使用者偏好顯示 12/24 小時制Display 12/24-hour format based on user preference
- 自動偵測使用者時區Auto-detect user timezone
- 提供時區選擇器(如果需要)Provide timezone selector if needed
💼 實際應用案例💼 Real-world Use Cases
案例 1:跨時區會議排程系統Case 1: Cross-timezone Meeting Scheduler
情境Scenario:開發一個支援全球用戶的會議排程系統: Developing a meeting scheduling system for global users
挑戰Challenges:
- 用戶分佈在不同時區(台北、紐約、倫敦)Users distributed across different timezones (Taipei, New York, London)
- 需要確保會議時間對所有人都正確Need to ensure meeting times are correct for everyone
- 處理夏令時間變化Handle daylight saving time changes
解決方案Solutions:
- 會議時間以 Unix 時間戳儲存在資料庫Store meeting times as Unix timestamps in the database
- 每個用戶的時區設定儲存在個人檔案Store each user's timezone setting in their profile
- 顯示時根據用戶時區轉換時間戳Convert timestamps based on user timezone when displaying
- 使用時間戳轉換器驗證不同時區的時間Use timestamp converter to verify times across timezones
範例Example:會議時間 = 1706342400(2024-01-27 08:00:00 UTC): Meeting time = 1706342400 (2024-01-27 08:00:00 UTC)
- 台北用戶看到:2024-01-27 16:00(下午4點)Taipei user sees: 2024-01-27 16:00 (4:00 PM)
- 紐約用戶看到:2024-01-27 03:00(凌晨3點)New York user sees: 2024-01-27 03:00 (3:00 AM)
- 倫敦用戶看到:2024-01-27 08:00(早上8點)London user sees: 2024-01-27 08:00 (8:00 AM)
案例 2:電商訂單追蹤Case 2: E-commerce Order Tracking
情境Scenario:電商平台需要記錄訂單的各個狀態時間點: E-commerce platform needs to record timestamps for various order statuses
挑戰Challenges:
- 訂單可能在不同國家處理Orders may be processed in different countries
- 需要精確計算處理時間Need to accurately calculate processing time
- 客戶服務需要查看易讀的時間Customer service needs human-readable times
解決方案Solutions:
- 訂單狀態變更時記錄 Unix 時間戳Record Unix timestamps when order status changes
- 計算處理時間:
shipped_at - created_atCalculate processing time: shipped_at - created_at
- 客戶端顯示轉換為本地時間Convert to local time for client display
- 報表系統統一使用 UTC 分析Use UTC consistently for reporting systems
訂單時間軸Order Timeline:
- 下單:1706342400(2024-01-27 16:00 台北)Order placed: 1706342400 (2024-01-27 16:00 Taipei)
- 付款:1706342500(2024-01-27 16:01:40 台北)Payment: 1706342500 (2024-01-27 16:01:40 Taipei)
- 出貨:1706378400(2024-01-28 02:00 台北)Shipped: 1706378400 (2024-01-28 02:00 Taipei)
- 送達:1706464800(2024-01-29 02:00 台北)Delivered: 1706464800 (2024-01-29 02:00 Taipei)
- 總處理時間Total processing time:122,400 秒 = 34 小時: 122,400 seconds = 34 hours
案例 3:社群媒體動態時間顯示Case 3: Social Media Post Timestamps
情境Scenario:社群平台需要顯示貼文的發佈時間: Social platform needs to display post publication times
挑戰Challenges:
- 用戶希望看到「相對時間」(幾分鐘前)Users prefer "relative time" (minutes ago)
- 舊貼文需要顯示完整日期Older posts need full date display
- 不同語言地區的時間格式不同Time formats vary by language/region
解決方案Solutions:
- 貼文時間以時間戳儲存Store post times as timestamps
- 前端計算當前時間與貼文時間差Frontend calculates difference between current time and post time
- 根據時間差選擇顯示格式Choose display format based on time difference
顯示規則Display Rules:
- < 1 分鐘:「剛剛」< 1 minute: "Just now"
- < 1 小時:「X 分鐘前」< 1 hour: "X minutes ago"
- < 24 小時:「X 小時前」< 24 hours: "X hours ago"
- < 7 天:「X 天前」< 7 days: "X days ago"
- > 7 天:「2024年1月27日」> 7 days: "January 27, 2024"
📚 相關資源📚 Related Resources
🌐 官方標準文件🌐 Official Standards
💻 程式庫與工具💻 Libraries & Tools
- Moment.js - JavaScript 時間處理庫(經典但已維護模式)JavaScript time library (classic but in maintenance mode)
- date-fns - 現代 JavaScript 日期工具庫Modern JavaScript date utility library
- Day.js - 輕量級時間處理庫Lightweight time handling library
- Arrow (Python) - Python 友好的時間處理庫Python-friendly time handling library
- Carbon (PHP) - PHP 日期時間處理擴充PHP date/time handling extension
🔗 相關工具與文章🔗 Related Tools & Articles
✨ 總結✨ Summary
時間戳轉換器是程式開發中不可或缺的工具。通過本指南,你已經掌握了:The timestamp converter is an essential tool in software development. Through this guide, you have learned:
- ✅ Unix 時間戳的概念與原理✅ The concept and principles of Unix timestamps
- ✅ 各種時間格式(ISO 8601、RFC 3339)的區別✅ The differences between various time formats (ISO 8601, RFC 3339)
- ✅ 時區轉換的正確方法✅ The correct methods for timezone conversion
- ✅ 主流程式語言的時間處理範例✅ Time handling examples in major programming languages
- ✅ 實際開發中的最佳實踐✅ Best practices for real-world development
記住,處理時間時應統一使用 UTC 儲存,顯示時轉換為本地時區,這樣可以避免大部分時區和夏令時間問題。需要快速轉換時間戳時,歡迎使用我們的線上時間戳轉換器!Remember, when handling time, store in UTC consistently and convert to local timezone for display - this avoids most timezone and DST issues. When you need to quickly convert timestamps, feel free to use our online timestamp converter!
🚀 立即使用時間戳轉換器🚀 Use Timestamp Converter Now
支援 Unix 時間戳、ISO 8601、多時區轉換,100% 本地處理,完全免費Supports Unix timestamps, ISO 8601, multi-timezone conversion, 100% local processing, completely free
免費使用時間戳轉換器 →Use Timestamp Converter Free →