Unix Timestamps Explained: Seconds, Milliseconds, Time Zones and Common Mistakes

Unix Timestamps Explained: Seconds, Milliseconds, Time Zones and Common Mistakes

Unix 时间戳详解:秒、毫秒、时区与常见错误

Unix timestamps are everywhere. APIs, databases, logs, JavaScript and authentication tokens all use them, and they cause a surprising number of bugs. Is 1790186400 a date? What about 1790186400000? Does a Unix timestamp contain a time zone? Why does JavaScript sometimes show a date in your local time when the timestamp itself is supposed to be UTC? This article explains how Unix timestamps actually work, the difference between seconds and milliseconds, how time zones fit into the picture, and the mistakes developers most commonly make.

Unix 时间戳无处不在。API、数据库、日志、JavaScript 和身份验证令牌都在使用它,但它也导致了惊人数量的 Bug。1790186400 是一个日期吗?那 1790186400000 呢?Unix 时间戳包含时区吗?为什么当时间戳本身应该是 UTC 时,JavaScript 有时会显示你本地时间的日期?本文将解释 Unix 时间戳的实际工作原理、秒与毫秒的区别、时区在其中的作用,以及开发者最常犯的错误。


What is a Unix timestamp?

什么是 Unix 时间戳?

A Unix timestamp represents the amount of time that has passed since January 1, 1970 at 00:00:00 UTC. This moment is commonly called the Unix epoch. For example, 0 represents: 1970-01-01 00:00:00 UTC. A timestamp such as 1790186400 represents a specific moment after the Unix epoch: 2026-09-23 18:00:00 UTC. The important part is that the timestamp represents an instant in time, not a formatted date. A value such as 2026-09-23 18:00:00 is a human-readable representation. A Unix timestamp is simply a number.

Unix 时间戳表示自 1970 年 1 月 1 日 00:00:00 UTC 以来所经过的时间。这一时刻通常被称为 Unix 纪元(Unix Epoch)。例如,0 代表:1970-01-01 00:00:00 UTC。像 1790186400 这样的时间戳代表了 Unix 纪元之后的一个特定时刻:2026-09-23 18:00:00 UTC。关键在于,时间戳代表的是时间轴上的一个瞬间,而不是一个格式化的日期。像 2026-09-23 18:00:00 这样的值是人类可读的表示形式,而 Unix 时间戳仅仅是一个数字。


Seconds vs milliseconds

秒与毫秒

One of the most common timestamp bugs comes from confusing seconds with milliseconds. Traditional Unix timestamps use seconds since January 1, 1970. Many programming environments, however, use milliseconds. The same instant looks like this in each unit: Seconds: 1790186400 Milliseconds: 1790186400000 Milliseconds usually have three extra digits. A quick rule of thumb: 10 digits -> usually seconds; 13 digits -> usually milliseconds. This works for modern dates, but you should not rely on digit count as formal validation.

最常见的时间戳 Bug 之一就是混淆了秒和毫秒。传统的 Unix 时间戳使用自 1970 年 1 月 1 日以来的秒数。然而,许多编程环境使用毫秒。同一个瞬间在不同单位下表现如下: 秒:1790186400 毫秒:1790186400000 毫秒通常多出三位数字。一个快速判断的经验法则:10 位数字 -> 通常是秒;13 位数字 -> 通常是毫秒。这对于现代日期有效,但你不应仅依赖数字位数作为正式的验证方式。


JavaScript uses milliseconds

JavaScript 使用毫秒

JavaScript’s Date API expects milliseconds: const timestamp = 1790186400000 const date = new Date(timestamp) console.log(date) But many APIs return Unix timestamps in seconds: const timestamp = 1790186400 If you pass that directly to Date, JavaScript interprets it as milliseconds, and the resulting date lands in January 1970: new Date(1790186400) // 1970-01-21T17:16:26.400Z Instead, convert seconds to milliseconds: const timestamp = 1790186400 const date = new Date(timestamp * 1000)

JavaScript 的 Date API 需要毫秒: const timestamp = 1790186400000 const date = new Date(timestamp) console.log(date) 但许多 API 返回的 Unix 时间戳是以秒为单位的: const timestamp = 1790186400 如果你直接将其传给 Date,JavaScript 会将其解释为毫秒,得到的日期会落在 1970 年 1 月: new Date(1790186400) // 1970-01-21T17:16:26.400Z 正确的做法是将秒转换为毫秒: const timestamp = 1790186400 const date = new Date(timestamp * 1000)


Does a Unix timestamp have a time zone?

Unix 时间戳有时区吗?

No. This is one of the most important things to understand about timestamps. A Unix timestamp does not contain a time zone. It represents one exact instant in time. For example, 1790186400 represents 2026-09-23 18:00:00 UTC. The exact same instant can be displayed as:

LocationLocal time
Calgary2026-09-23 12:00:00 MDT
New York2026-09-23 14:00:00 EDT
London2026-09-23 19:00:00 BST
The timestamp does not change. Only its formatted representation changes. Think of a Unix timestamp as a position on a global timeline. A time zone only changes how that position is displayed to a person.

没有。这是关于时间戳最重要的一点。Unix 时间戳不包含时区,它代表时间轴上的一个精确瞬间。例如,1790186400 代表 2026-09-23 18:00:00 UTC。同一个瞬间可以显示为:

地点本地时间
卡尔加里2026-09-23 12:00:00 MDT
纽约2026-09-23 14:00:00 EDT
伦敦2026-09-23 19:00:00 BST
时间戳本身不会改变,改变的只是它的格式化表示。可以将 Unix 时间戳想象成全球时间轴上的一个位置,时区只会改变该位置向用户展示的方式。