Unix Timestamp Converter — Developer Code Samples
Unix timestamps represent seconds (or milliseconds) elapsed since January 1, 1970 UTC. Python's datetime module and JavaScript's Date object provide full conversion, formatting, and timezone-aware operations. Essential for log analysis and API development.
Try the interactive version online:
Open Unix Timestamp Converter Tool →
Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
| timestamp | int | Yes | Unix timestamp in seconds (or milliseconds if > 1e10) |
| timezone | str | No | Target timezone name or UTC offset (default: UTC) |
| format | str | No | Output format string (strftime syntax, default: ISO 8601) |
Returns: Dict with iso8601, human_readable, local_time, day_of_week, and relative_time fields
Code Examples
from datetime import datetime, timezone, timedelta
import time
# Get current Unix timestamp
now_ts = int(time.time())
print(f"Current timestamp: {now_ts}")
# Convert Unix timestamp to readable date (UTC)
ts = 1700000000
dt_utc = datetime.fromtimestamp(ts, tz=timezone.utc)
print(dt_utc.isoformat()) # 2023-11-14T22:13:20+00:00
print(dt_utc.strftime('%Y-%m-%d %H:%M:%S UTC'))
# Convert to local time
dt_local = datetime.fromtimestamp(ts)
print(dt_local.strftime('%Y-%m-%d %H:%M:%S (local)'))
# Convert to a specific timezone
eastern = timezone(timedelta(hours=-5)) # UTC-5 (EST)
dt_eastern = datetime.fromtimestamp(ts, tz=eastern)
print(dt_eastern.strftime('%Y-%m-%d %H:%M:%S EST'))
# Convert datetime to Unix timestamp
dt = datetime(2024, 1, 15, 12, 0, 0, tzinfo=timezone.utc)
ts_back = int(dt.timestamp())
print(ts_back)
# Millisecond timestamp (JavaScript-style)
ts_ms = int(time.time() * 1000)
print(f"Millisecond timestamp: {ts_ms}")
dt_from_ms = datetime.fromtimestamp(ts_ms / 1000, tz=timezone.utc)
print(dt_from_ms.isoformat())
# Format with strftime
print(dt_utc.strftime('%B %d, %Y at %I:%M %p UTC')) # November 14, 2023 at 10:13 PM UTC