DevToolsForYou

Unix Timestamp Converter in Python — Code Examples

Unix Timestamp Converter in PythonUse the online tool →

A Unix timestamp is the number of seconds (or milliseconds) since 1 January 1970 00:00:00 UTC. Here is how to get the current timestamp, convert to a human-readable date, and parse date strings back to timestamps in each language.

Python's datetime module and time module both handle Unix timestamps. Use datetime.fromtimestamp() for local time and datetime.utcfromtimestamp() for UTC.

Python 3
import time
from datetime import datetime, timezone

# Current timestamp
now_sec = int(time.time())            # seconds
now_ms  = int(time.time() * 1000)    # milliseconds

# Timestamp → datetime (UTC)
dt_utc = datetime.fromtimestamp(1700000000, tz=timezone.utc)
print(dt_utc)                  # 2023-11-14 22:13:20+00:00
print(dt_utc.isoformat())      # 2023-11-14T22:13:20+00:00

# Timestamp → datetime (local time)
dt_local = datetime.fromtimestamp(1700000000)
print(dt_local)

# datetime → timestamp
ts = datetime(2023, 11, 14, 22, 13, 20, tzinfo=timezone.utc).timestamp()
print(int(ts))  # 1700000000

# Format
print(dt_utc.strftime("%Y-%m-%d %H:%M:%S UTC"))
Notes & gotchas
  • Always pass tz=timezone.utc to fromtimestamp() for UTC output — the bare call uses local time.
  • datetime objects without tzinfo are 'naive' — attach timezone.utc for reliable arithmetic.
  • For advanced timezone support (IANA zones), use the zoneinfo module (Python 3.9+) or pytz.
Try it in your browser

Need to epoch/unix converter without writing code? The Unix Timestamp Converter runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Epoch/Unix Converter
Unix Timestamp Converter in other languages