Python Epoch & Timestamp Converter
Convert between epoch timestamps and human-readable dates for Python. Test conversions instantly, then use the code examples with datetime, time, and dateutil in your application. All conversions happen in your browser.
Epoch / Timestamp Converter
Convert between Unix timestamps and human-readable dates. Supports seconds and milliseconds.
Quick Reference
Unix epoch is the number of seconds since January 1, 1970 00:00:00 UTC. Timestamps with 13+ digits are automatically treated as milliseconds. Negative values represent dates before 1970.
How to convert epoch timestamps in Python
Get current epoch: import time; epoch = int(time.time()). Epoch to datetime: from datetime import datetime; dt = datetime.fromtimestamp(1616000000). Datetime to epoch: int(datetime(2024, 1, 15, 12, 0).timestamp()). For millisecond timestamps (JavaScript-style): datetime.fromtimestamp(1616000000000 / 1000). For UTC explicitly: datetime.utcfromtimestamp(epoch) or datetime.fromtimestamp(epoch, tz=timezone.utc). Always use timezone-aware datetimes in production to avoid bugs.
# Python — epoch timestamp conversion
from datetime import datetime, timezone
import time
# Current epoch timestamp
now_epoch = int(time.time())
print(now_epoch) # 1711230000
# Epoch → datetime
dt = datetime.fromtimestamp(1711230000, tz=timezone.utc)
print(dt) # 2025-03-23 18:00:00+00:00
print(dt.strftime("%Y-%m-%d %H:%M:%S")) # "2025-03-23 18:00:00"
# Datetime → epoch
epoch = int(dt.timestamp())
print(epoch) # 1711230000
# Milliseconds (JavaScript-style)
ms_epoch = int(time.time() * 1000)
print(ms_epoch) # 1711230000000Timezone handling with Python timestamps
Python's datetime is timezone-naive by default — a common source of bugs. Use timezone-aware datetimes: from datetime import timezone; dt = datetime.now(timezone.utc). With pytz: import pytz; eastern = pytz.timezone('US/Eastern'); dt = datetime.now(eastern). With zoneinfo (Python 3.9+): from zoneinfo import ZoneInfo; dt = datetime.now(ZoneInfo('America/New_York')). Convert between timezones: dt.astimezone(ZoneInfo('Europe/London')). Store timestamps as UTC epoch integers in databases, convert to local time only for display.
Date formatting and parsing in Python
Format dates: dt.strftime('%Y-%m-%d %H:%M:%S') returns '2024-01-15 12:00:00'. Parse dates: datetime.strptime('2024-01-15', '%Y-%m-%d'). ISO 8601 format: dt.isoformat() returns '2024-01-15T12:00:00+00:00'. Parse ISO 8601: datetime.fromisoformat('2024-01-15T12:00:00+00:00'). For complex date parsing, use python-dateutil: from dateutil.parser import parse; dt = parse('January 15, 2024 12:00 PM EST'). Common strftime codes: %Y (4-digit year), %m (month), %d (day), %H (24h hour), %M (minute), %S (second), %z (timezone offset).
Frequently Asked Questions
How do I get the current epoch timestamp in Python?
Use int(time.time()) for seconds since Unix epoch (January 1, 1970). For milliseconds: int(time.time() * 1000). For a timezone-aware datetime: datetime.now(timezone.utc).timestamp().
What is the difference between fromtimestamp() and utcfromtimestamp()?
fromtimestamp(epoch) returns a naive datetime in your local timezone. utcfromtimestamp(epoch) returns a naive datetime in UTC. Both are naive (no timezone info). Best practice: datetime.fromtimestamp(epoch, tz=timezone.utc) which returns a timezone-aware UTC datetime.
How do I handle millisecond timestamps in Python?
JavaScript and many APIs use millisecond epochs. Divide by 1000: datetime.fromtimestamp(ms_epoch / 1000). To generate millisecond epochs: int(time.time() * 1000). Be careful: if a timestamp has 13 digits, it is milliseconds; 10 digits means seconds.
Related Convert Tools
URL Encoder & Decoder
Encode and decode URLs with encodeURIComponent and encodeURI
JSON ↔ YAML Converter
Convert between JSON and YAML for Kubernetes, Docker, and CI/CD configs
HTML Entity Encoder
Encode and decode HTML entities, special characters, and symbols
Image to Base64
Convert images to Base64 data URIs or decode Base64 back to images