转换 between timezones correctly
Timezone conversion 是 tricky 由于 DST, abbreviations, 和 political changes. Learn 到 use IANA timezone names 和 Instant/ZonedDateTime 模式.
- 1
Best practices: (1) Store timestamps 在 UTC. (2) Use IANA timezone names (America/New_York, 不 EST/EDT — abbreviations 是 ambiguous). (3) Convert 到本地时间仅 为 display. (4) Use 库 (日期-fns-tz, Luxon, js-joda) 该 supports IANA timezones. (5) 从不 use 日期为 timezone math — JavaScript 日期是 broken 为 timezones. (6) 是 aware 的 DST transitions (2 AM becomes 3 AM 在 spring, 2 AM repeats 在 fall).
- 2 时区转换器 →
将 a timestamp from one timezone to another. Specify the source and target IANA timezone names (e.g., America/New_York 转换为 Asia/Tokyo). The tool handles DST transitions correctly. Always use IANA names, not abbreviations (EST could be US Eastern or Australian Eastern)
- 3 时间戳转换器 →
获取 the current UTC timestamp. Store this in databases, APIs, and logs. UTC is universal — no DST, no political changes. Convert to local time only at the presentation layer. Use ISO 8601 format (2024-01-15T10:30:00Z) for storage and exchange.
- 4 日期计算器 →
计算 date differences across timezones. Be careful: "add 1 day" in UTC may cross a DST boundary in local time, resulting in 23 or 25 hours. Use date-based arithmetic (add days, not hours) when the intent is calendar-based, and time-based arithmetic (add hours) when the intent is duration-based.
常见问题
- 为什么是 timezone abbreviations ambiguous?
- EST 是两者我们 Eastern 标准 Time (UTC-5) 和 Australian Eastern 标准 Time (UTC+10). CST 是我们中心, China 标准, 和 Cuba 标准 Time. IANA names (America/New_York, Asia/Shanghai) 是 unambiguous. 总是 use IANA names 为 storage 和 computation; use abbreviations 仅为 display.
- 如何 I 处理 DST transitions?
- Spring forward: 2 AM becomes 3 AM — times 之间 2 和 3 AM 不存在. Fall 回: 2 AM becomes 1 AM — times 之间 1 和 2 AM 发生两次. 为 scheduling, use UTC 到 avoid ambiguity. 为 display, 好库 将 show 哪个 occurrence (之前或 之后 transition) 你 mean. 从不调度 events 在 2:30 AM — 它可以不 存在或 可以是 ambiguous.