Unix Timestamp in Python

Python returns epoch time as a float in seconds. The modern approach is timezone-aware datetime objects with an explicit tz; naive datetimes are the root of most Python date bugs.

Need to convert one value rather than write code? Use the interactive converter — it handles every unit and time zone without leaving your browser.

Current Unix timestamp

import time
from datetime import datetime, timezone

print(int(time.time()))                    # 1710050400  (seconds)
print(time.time_ns() // 1_000_000)         # 1710050400000  (milliseconds)
print(int(datetime.now(timezone.utc).timestamp()))

Timestamp to date

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

ts = 1710050400

# Always pass tz — datetime.utcfromtimestamp() is deprecated and returns
# a naive object that silently misbehaves in later arithmetic.
utc = datetime.fromtimestamp(ts, tz=timezone.utc)
print(utc.isoformat())                 # 2024-03-10T06:00:00+00:00

local = datetime.fromtimestamp(ts, tz=ZoneInfo("Asia/Kolkata"))
print(local.strftime("%Y-%m-%d %H:%M:%S %Z"))

Date to timestamp

from datetime import datetime
from zoneinfo import ZoneInfo

dt = datetime(2024, 3, 10, 11, 30, 0, tzinfo=ZoneInfo("Asia/Kolkata"))
print(int(dt.timestamp()))             # 1710050400

# Parsing an ISO 8601 string (3.11+ handles the trailing Z)
parsed = datetime.fromisoformat("2024-03-10T06:00:00+00:00")
print(int(parsed.timestamp()))

Milliseconds and pandas

import pandas as pd

# Whole column at once — unit is explicit, never guessed
s = pd.Series([1710050400000, 1710136800000])
print(pd.to_datetime(s, unit="ms", utc=True).dt.tz_convert("Europe/London"))

Pitfalls specific to Python

  • datetime.utcnow() and datetime.utcfromtimestamp() are deprecated in 3.12 — they return naive objects that claim to be UTC but carry no tzinfo, so .timestamp() on them re-interprets the value in the system zone.
  • time.time() is a float, so int() truncates rather than rounds. That is usually what you want.
  • Use zoneinfo from the standard library rather than pytz; pytz needs the localize() dance and is easy to get wrong.

Rules that apply in every language

  1. Store UTC, display local. Keep the instant in UTC everywhere in your system and convert only at the point a human reads it.
  2. Name the unit in the identifier. expires_at_ms rather than expires_at costs nothing and prevents the single most common timestamp bug.
  3. Never trust a client clock. Stamp anything security-relevant on the server. See the note on clock skew.
  4. Use 64-bit time. Anything still storing seconds in a signed 32-bit field breaks in January 2038 — see the Year 2038 problem.

The same task in other languages

Frequently asked questions

How do I get the current Unix timestamp in Python?

Use the snippet in the "Current Unix timestamp" section above. Python works in float seconds natively, so converting to another unit is a multiplication or an integer division away.

How do I convert a Unix timestamp to a date in Python?

The "Timestamp to date" snippet above shows the idiomatic approach, including how to render the result in a specific time zone rather than whatever zone the machine happens to be set to.

Does Python handle time zones and daylight saving correctly?

Yes, provided you pass an explicit zone rather than relying on the system default. The gotchas listed on this page cover the specific ways Python makes that easy to get wrong.