Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

JavaScript Unix Timestamps: Seconds, Dates, Time Zones

· 4 min read · Home Tech

In this article
  1. Formula
  2. Worked Example
  3. Why Time Zone Errors Happen
  4. How to Convert Safely
  5. Common Pitfalls and Misconceptions
  6. Alternatives and Libraries
  7. Performance and Precision
  8. Frequently Asked Questions

This page explains how to convert Unix timestamps (seconds since 1970-01-01 UTC) into readable dates in JavaScript, and how to avoid the time zone errors that commonly occur during conversion. You enter a numeric timestamp and the code below returns the corresponding UTC and local date-time strings, showing exactly where time zone adjustments happen.

in ts | Timestamp (seconds) | s | 1700000000 | 0
out utc | UTC date-time | - | new Date(ts * 1000).toISOString() | 0
out local | Local date-time | - | new Date(ts * 1000).toString() | 0

Formula

The core conversion is:

date = new Date(timestamp * 1000)

Because JavaScript's Date object works in milliseconds, multiply the Unix timestamp (seconds) by 1000. The resulting Date object stores an absolute instant in time, independent of any time zone. Then:

  • toISOString() returns the date-time in UTC (with a Z suffix).
  • toString() returns the date-time in the browser's local time zone.
  • getTime() returns the milliseconds since the Unix epoch, which you can divide by 1000 to get back the original timestamp.

Worked Example

Using the default timestamp 1700000000 seconds:

  1. Multiply by 1000: 1700000000 * 1000 = 1700000000000 ms.
  2. Create a Date object: new Date(1700000000000).
  3. Call toISOString(): returns 2023-11-14T22:13:20.000Z (UTC).
  4. Call toString(): returns something like Tue Nov 14 2023 17:13:20 GMT-0500 (Eastern Standard Time) for a user in New York.

The calculator's first output shows the UTC string; the second shows the local string, which varies by the viewer's time zone.

Why Time Zone Errors Happen

The most common mistake is assuming a timestamp is in local time. A Unix timestamp is always UTC-based. If you treat it as local time and convert it directly, you shift the date. For example, 1700000000 corresponds to 22:13 UTC. If you incorrectly parse that as local time, you get the wrong hour and possibly the wrong day. Another frequent error is using new Date(timestamp) without multiplying by 1000 when the timestamp is in seconds, which yields a date in 1970.

How to Convert Safely

Follow these steps to avoid errors:

  • Always confirm the unit of your timestamp: seconds, milliseconds, or microseconds. Most APIs give seconds, but some give milliseconds.
  • Multiply seconds by 1000 before passing to new Date().
  • Use toISOString() for a fixed, time-zone-independent representation.
  • Use toLocaleString() or toString() only when you want the user's local time.
  • When sending dates to a server, send the numeric timestamp or the ISO string, not a local string.

Common Pitfalls and Misconceptions

Misconception: getTime() returns local time. No, it returns milliseconds since the epoch, which is the same everywhere. The time zone only affects how you format that instant.

Misconception: toISOString() changes the date. It does not; it simply displays the same instant in UTC. If you compare it to a local string, they look different but represent the same moment.

Pitfall: Using new Date('2023-11-14T22:13:20') without a time zone. JavaScript treats date-only strings as UTC, but date-time strings without a zone as local time. This inconsistency causes many bugs. Always include the Z or an offset.

Alternatives and Libraries

For simple conversions, vanilla JavaScript is sufficient. For complex time zone handling (e.g., converting to a specific zone like Asia/Tokyo), libraries like date-fns-tz or Luxon provide robust helpers. However, for most use cases, the built-in Date methods are enough. The Intl.DateTimeFormat API also allows formatting in any time zone without a library.

Performance and Precision

Date parsing and formatting are fast, but if you process millions of timestamps, consider using Date.UTC() for construction and Intl.DateTimeFormat for formatting, as they are optimized. Precision is millisecond-level; if you need microseconds, you must handle them separately.

Frequently Asked Questions

Why do I get a date in 1970?

You likely forgot to multiply by 1000. A timestamp like 1700000000 milliseconds is only about 19.7 days after the epoch, not 2023. Always check the unit.

What is the difference between toISOString() and toString()?

toISOString() always returns UTC in ISO 8601 format (e.g., 2023-11-14T22:13:20.000Z). toString() returns a human-readable string in the local time zone of the browser (e.g., Tue Nov 14 2023 17:13:20 GMT-0500).

How do I convert a Unix timestamp to a specific time zone?

Use toLocaleString('en-US', { timeZone: 'America/New_York' }). The Intl API supports any IANA time zone name. This avoids manual offset calculations.

Is a Unix timestamp affected by daylight saving time?

No. The timestamp is an absolute count of seconds since 1970-01-01 UTC. DST only affects how you display that instant in a local time zone. The numeric value never changes.

Can I store dates as timestamps in a database?

Yes, storing as an integer (seconds or milliseconds) is common and avoids time zone ambiguity. However, for human-readable queries, you may want to store ISO strings. Both are valid; choose based on your query needs.

Why does my date show one day off?

This usually happens when you parse a date string without a time zone and then convert it. For example, new Date('2023-11-14') is UTC midnight, while new Date('2023-11-14 00:00') is local midnight. Mixing these causes shifts.