DevBolt
Processed in your browser. Your data never leaves your device.

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.

← Back to tools

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.

Timezone 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