Python Unix Timestamp Converter

Convert Unix timestamps in Python with live parser and datetime/timezone snippets. UTC-aware patterns included.

⏱ UnixLi — Live Tool All processing local · No data sent
—
unix seconds · now

Smart Universal Parser

// Paste Unix seconds/ms, ISO 8601, RFC 2822, or a human date.

Developer Snippets

// Copy-ready code — current timestamp injected automatically.

Unix timestamps in Python

Python gives you two layers for time: the low-level time module, which returns raw epoch numbers, and the datetime module, which builds calendar objects. The safe modern pattern is simple: get epoch seconds, convert them to a timezone-aware datetime in UTC, and change zones only when displaying. Use the live parser above to check any value, then copy the Python snippet it generates. Parsing happens in your browser; pasted timestamps are never uploaded.

Current time: time.time() and friends

time.time() returns seconds since the epoch as a float with sub-second precision. Wrap it in int() for whole Unix seconds. time.time_ns() returns an integer number of nanoseconds, which avoids float rounding when you need milliseconds:

Python
import time

seconds = int(time.time())             # e.g. 1715429912
millis = time.time_ns() // 1_000_000   # integer milliseconds

Epoch to datetime with fromtimestamp (timezone-aware)

Always pass tz=timezone.utc. Without it, datetime.fromtimestamp() returns a naive datetime in the server’s local timezone, so the same code prints different results on your laptop and in production.

Python 3.9+
from datetime import datetime, timezone
from zoneinfo import ZoneInfo

dt = datetime.fromtimestamp(1715429912, tz=timezone.utc)
print(dt.isoformat())                         # 2024-05-11T12:18:32+00:00
print(dt.astimezone(ZoneInfo("Asia/Tokyo")))  # 2024-05-11 21:18:32+09:00
print(int(dt.timestamp()))                    # 1715429912

That confirms 1715429912 is Saturday, May 11, 2024 at 12:18:32 UTC. For milliseconds, divide first: datetime.fromtimestamp(ms / 1000, tz=timezone.utc).

Why utcfromtimestamp() is deprecated

datetime.utcfromtimestamp() and datetime.utcnow() are deprecated since Python 3.12. They return naive datetimes that hold UTC values but carry no timezone, so later calls like .timestamp() treat them as local time and shift the result by your offset. Replace them with datetime.fromtimestamp(ts, tz=timezone.utc) and datetime.now(timezone.utc). (datetime.UTC is an alias for timezone.utc on Python 3.11+.)

Time zones with zoneinfo

Since Python 3.9, the standard library includes zoneinfo, which reads the IANA timezone database. It replaces most uses of pytz and handles daylight saving time correctly when you call astimezone(). On Windows or slim containers without system tz data, install the tzdata package from PyPI.

Datetime to epoch

Python
from datetime import datetime, timezone

aware = datetime(2024, 5, 11, 12, 18, 32, tzinfo=timezone.utc)
int(aware.timestamp())                                          # 1715429912
int(datetime.fromisoformat("2024-05-11T12:18:32Z").timestamp())  # 1715429912 (3.11+)

Calling .timestamp() on a naive datetime assumes local time, a quiet source of off-by-hours bugs. Keep datetimes aware from creation to storage. The full walkthrough, including pandas and string formatting, is in Convert a timestamp to a date in Python. If your data lives in Postgres, compare with the PostgreSQL timestamp guide.

pandas and data pipelines

In data work, epoch columns usually arrive as integers. pd.to_datetime(df["ts"], unit="s", utc=True) converts a whole column to timezone-aware UTC timestamps in one vectorized call. Use unit="ms" for JavaScript-style milliseconds. Passing utc=True matters for the same reason tz=timezone.utc does in plain Python: naive values end up interpreted inconsistently later. Convert to a display zone at the end with .dt.tz_convert("Europe/Paris").

Common Python timestamp pitfalls

Frequently asked questions

How do I get the current Unix timestamp in Python?

Use int(time.time()) for seconds, or time.time_ns() // 1_000_000 for integer milliseconds.

How do I convert a Unix timestamp to a datetime in UTC?

Call datetime.fromtimestamp(ts, tz=timezone.utc). For 1715429912 this returns 2024-05-11 12:18:32+00:00.

Is datetime.utcfromtimestamp() deprecated?

Yes, since Python 3.12, together with utcnow(). Both return naive datetimes. Use datetime.fromtimestamp(ts, tz=timezone.utc) and datetime.now(timezone.utc) instead.

Should I use zoneinfo or pytz?

For Python 3.9+, use the standard-library zoneinfo with IANA names such as ZoneInfo('Europe/Paris'). Install tzdata if the system has no timezone database.

Why is my datetime.timestamp() off by several hours?

The datetime is probably naive, so Python treats it as local time. Attach tzinfo=timezone.utc, or build it with an aware constructor, before calling .timestamp().

Are my timestamps processed privately?

Yes. The converter on this page runs in your browser and never sends pasted values to a server.

Related tools