Unix 纪元与日期格式转换器

在 Unix 时间戳、ISO 8601、RFC 2822、SQL 日期和 Excel 序列号之间转换,覆盖你会遇到的所有时间戳格式。

The formats you will actually meet

FormatExampleWhere it appears
Unix seconds1736899200APIs, logs, databases
Unix milliseconds1736899200000JavaScript Date.now(), most modern APIs
ISO 86012025-01-15T10:30:00ZJSON, most modern systems
RFC 2822Wed, 15 Jan 2025 10:30:00 GMTHTTP headers, email
RFC 33392025-01-15T10:30:00+00:00Strict ISO profile with offset
SQL datetime2025-01-15 10:30:00MySQL, PostgreSQL
Excel serial45674Spreadsheets

Seconds or milliseconds?

The digit count tells you. Ten digits is seconds, thirteen digits is milliseconds. JavaScript's Date.now() returns milliseconds, while Math.floor(Date.now() / 1000) returns seconds. Mixing them up is the single most common timestamp bug.

// Wrong - a 1000x error
new Date(1736899200)  // 1970
new Date(1736899200000)  // 2025

// Detect safely
const ms = Math.abs(t) < 1e11 ? t * 1000 : t;

The Excel serial number

Excel counts days from 30 December 1899 — an arbitrary choice that includes a deliberate bug for backwards compatibility with Lotus 1-2-3. Serial 1 is 1 January 1900, and 60 is the non-existent 29 February 1900. This is why the epoch for Excel conversions is 1899-12-30 rather than 1900-01-01.

Timezones

A Unix timestamp is always UTC — it has no timezone attached. When you display it, you convert. The tool shows both the UTC value and your browser's local time.

To convert in JavaScript:

new Date(ms).toISOString();                  // UTC, always
new Date(ms).toLocaleString();               // local
new Date(ms).toLocaleString('en-GB', { timeZone: 'Asia/Tokyo' });

The 2038 problem

Timestamps stored as 32-bit signed integers overflow on 19 January 2038. If you maintain software with a long life ahead of it, use 64-bit integers or ISO 8601 strings.

常见问题

Is my timestamp sent anywhere?

No. The conversion happens entirely in your browser using the built-in Date object.

How does it tell seconds from milliseconds?

By the digit count. Values under 1e11 are treated as seconds and multiplied by 1000; anything larger is treated as milliseconds already.

What timezone is the output in?

The timestamp is always UTC, since that is what a Unix timestamp represents. The output shows the UTC value plus your browser's local time so you can verify it.

Why is the Excel epoch 1899?

Excel dates count from 30 December 1899 for backwards compatibility with Lotus 1-2-3, and it contains a deliberate leap-year bug for 1900. That is why conversions use 1899-12-30 as the reference.

What is the 2038 problem?

Timestamps stored in 32-bit signed integers overflow on 19 January 2038. Use 64-bit integers or ISO 8601 strings for anything long-lived.