⚡Utilvy
2026-11-18

Convert Unix Timestamps in Python, JavaScript & SQL

A Unix timestamp is just the number of seconds elapsed since 1970-01-01 00:00:00 UTC, so converting it takes one line in any language: int(time.time()) in Python, Math.floor(Date.now() / 1000) in JavaScript, and UNIX_TIMESTAMP() in MySQL. This guide gives copy-paste examples for each language, plus the classic pitfalls that turn 2025 dates into 1970.

How Unix Timestamp Conversion Works (and Where It Breaks)

Every Unix timestamp counts elapsed seconds from the epoch: midnight UTC on January 1, 1970. The value is timezone-neutral, which is exactly why it works well for storage and APIs. The confusion starts because different systems disagree on units and on what happens at the boundaries:

  • Seconds vs milliseconds. JavaScript's Date.now() returns milliseconds, while Python's time.time() returns seconds. Mixing them up shifts your date by three digits, or about 31,688 years.
  • UTC vs local time. A timestamp always represents the same instant worldwide. 0 is not "midnight in New York" — it is 1970-01-01T00:00:00Z. Local time only appears when you format the value.
  • Data types. Storing timestamps as 32-bit integers caps you at 2,147,483,647 seconds, which breaks on January 19, 2038 (the "Y2038" problem).

Here is the same moment — 2025-01-01 00:00:00 UTC — as it appears in each language:

| Language | Timestamp value | Unit | |---|---|---| | Python time.time() | 1735689600.0 | seconds | | JavaScript Date.now() | 1735689600000 | milliseconds | | MySQL UNIX_TIMESTAMP('2025-01-01') | 1735689600 | seconds | | PostgreSQL EXTRACT(EPOCH ...) | 1735689600 | seconds | | SQLite strftime('%s', ...) | 1735689600 | seconds |

Notice only JavaScript is in milliseconds by default. This single fact causes most "my date is in 1970" bugs.

One more quirk worth knowing: Unix time ignores leap seconds, so it is not a strict measure of physical elapsed time. It also handles dates before 1970 with negative values — Python and PostgreSQL accept them happily, while some older libraries and embedded systems do not. None of this affects everyday work, but it explains why two systems can occasionally disagree about an instant by one second during leap-second adjustments.

Step-by-Step: Convert Timestamps in Each Language

Python

Get the current timestamp and convert both directions:

import time
from datetime import datetime, timezone

now = int(time.time())                          # e.g. 1735689600
dt = datetime.fromtimestamp(now, tz=timezone.utc)
print(dt.isoformat())                           # 2025-01-01T00:00:00+00:00

back = int(dt.timestamp())                      # date -> timestamp

Always pass tz=timezone.utc when converting a raw timestamp to a datetime. Without it, Python uses the server's local timezone, and a value that is correct UTC midnight can render as the previous day's evening — a common cause of off-by-one-day reports.

JavaScript

const nowSec = Math.floor(Date.now() / 1000);   // seconds, e.g. 1735689600

const d = new Date(nowSec * 1000);              // seconds -> Date object
console.log(d.toISOString());                   // 2025-01-01T00:00:00.000Z

new Date() accepts either a number of milliseconds or an ISO string — but not seconds. If you pass seconds directly, JavaScript treats them as milliseconds and produces a date 31,000 years in the future. Multiplying by 1000 before constructing the Date is the fix. To convert a Date back to seconds, use Math.floor(d.getTime() / 1000).

SQL

MySQL:

SELECT UNIX_TIMESTAMP();                        -- current timestamp
SELECT FROM_UNIXTIME(1735689600);               -- 2025-01-01 00:00:00

PostgreSQL:

SELECT EXTRACT(EPOCH FROM now())::bigint;
SELECT to_timestamp(1735689600);                -- timestamptz

SQLite:

SELECT strftime('%s', 'now');                   -- current, seconds
SELECT datetime(1735689600, 'unixepoch');       -- 2025-01-01 00:00:00

In MySQL, FROM_UNIXTIME renders in the session's time zone, so two connections with different time_zone settings will show different strings for the same value. PostgreSQL's to_timestamp() returns timestamptz and displays per the session zone as well. When in doubt, run the query in UTC.

Frequently Asked Questions

Why does my converted date show January 1970?

That usually means you converted 0 or a tiny number — the classic symptom is dividing or multiplying in the wrong direction. If your date is near 1970-01-20, you converted a seconds-based value that was actually milliseconds divided by 1000; if it is tens of thousands of years away, you passed seconds where milliseconds were expected. Check the digit count: a seconds timestamp is 10 digits until the year 2286; milliseconds is 13 digits. A quick sanity check in a timestamp converter tool will confirm which unit you actually have.

How do I convert a timestamp with a timezone offset?

Do not add or subtract hours by hand. First interpret the timestamp as UTC (a Unix timestamp has no offset — the offset belongs to the display). Then format it in the target zone: in Python use datetime.fromtimestamp(ts, tz=ZoneInfo("America/New_York")); in JavaScript use toLocaleString("en-US", { timeZone: "America/New_York" }); in SQL, set the session time zone before calling FROM_UNIXTIME or to_timestamp.

Should I store dates as timestamps or as date types?

Timestamps are ideal for event times, logs, and APIs because they are unambiguous and sort naturally. But they cannot represent dates before 1970 negatively-friendly in some legacy systems, and they store no calendar concepts. For birthdays, deadlines, and schedules — where "the same date" matters across timezones — a DATE column is safer. A common hybrid is to store UTC timestamps for events and local DATE values for human-scheduled things.

Convert Timestamps Without Writing Code

When you just need to check one value — debugging an API response, decoding a log entry, or verifying a database row — a dedicated converter is faster than firing up an interpreter. The Unix timestamp converter on Utilvy turns timestamps into readable dates in any timezone and back again instantly, and it handles both seconds and milliseconds automatically. For generating unique, sortable identifiers to sit alongside your event timestamps, the UUID generator produces UUIDv4 and UUIDv7 in one click.

Try these tools