Unix Timestamp Cheat
Quick reference for Unix timestamp and date formats.
Unix Timestamp Cheat
A Unix timestamp is the number of seconds since January 1, 1970 00:00:00 UTC (the “epoch”). Some APIs use milliseconds. Always check which one.
Landmarks: 0 is Jan 1, 1970. 86400 is one day later. 1704067200 is Jan 1, 2024 00:00:00 UTC.
Get “now” in code
JavaScript gives milliseconds. For seconds, divide by 1000 and floor.
Date.now() // 1734567890123 (ms)
Math.floor(Date.now() / 1000) // 1734567890 (seconds, e.g. for API auth)Python and PHP use seconds by default.
import time
time.time() # 1734567890.123time(); // 1734567890Turn a timestamp into a date
JavaScript’s Date constructor expects milliseconds. If you have seconds (e.g. from an API), multiply by 1000.
const sec = 1704067200;
new Date(sec * 1000).toISOString(); // "2024-01-01T00:00:00.000Z"
new Date(sec * 1000).toLocaleString(); // locale date/time stringPython:
from datetime import datetime, timezone
datetime.fromtimestamp(1704067200, tz=timezone.utc)
# datetime(2024, 1, 1, 0, 0, tzinfo=timezone.utc)Turn a date string into a timestamp
ISO 8601 format: YYYY-MM-DDTHH:mm:ss.sssZ. JavaScript:
new Date('2024-01-01T00:00:00.000Z').getTime() // ms
Math.floor(new Date('2024-01-01T00:00:00.000Z').getTime() / 1000) // secondsPython:
from datetime import datetime, timezone
dt = datetime.fromisoformat('2024-01-01T00:00:00+00:00')
int(dt.timestamp()) # 1704067200Today at midnight UTC
Useful for “start of day” buckets or cache keys that change daily.
from datetime import datetime, timezone
start = datetime.now(timezone.utc).replace(hour=0, minute=0, second=0, microsecond=0)
int(start.timestamp())JavaScript:
const d = new Date();
d.setUTCHours(0, 0, 0, 0);
Math.floor(d.getTime() / 1000);Difference in days
Subtract two timestamps (same unit). Divide by 86400 for days.
const old = 1704067200; // Jan 1, 2024
const now = Math.floor(Date.now() / 1000);
const daysAgo = Math.floor((now - old) / 86400);ISO 8601 quick reference
Format: YYYY-MM-DDTHH:mm:ss.sssZ. The Z means UTC. Example: 2024-01-01T00:00:00.000Z. Use this when talking to APIs or storing dates in a portable way.
Timezone gotchas
Unix timestamps are always in UTC. When you create a date from a timestamp, the result can be in local time or UTC depending on the API. In JavaScript, new Date(sec * 1000) creates a date in the local timezone for display. Use toISOString() for UTC. In Python, datetime.fromtimestamp(ts) gives local time by default. Use datetime.fromtimestamp(ts, tz=timezone.utc) for UTC. When comparing or storing, stick to one convention (usually UTC) and convert to local only for display.
Local midnight vs UTC midnight. “Start of today” in the user’s timezone is different from “start of today UTC”. For cache keys or daily buckets that should be global, use UTC midnight. For “today’s events” in a calendar, use local midnight (e.g. in JavaScript construct the date at midnight in local time, then get the timestamp).
Start of week (Monday)
Useful for weekly reports or “this week” filters. Get today, go back to the most recent Monday, set time to midnight, then take the timestamp. In Python with datetime: subtract the weekday (0=Monday, 6=Sunday) in days and zero out the time. In JavaScript: similar logic with getDay() (0=Sunday) so you need to map to Monday-based.
from datetime import datetime, timezone, timedelta
now = datetime.now(timezone.utc)
# Monday = 0, so weekday() gives days since Monday
start_of_week = (now - timedelta(days=now.weekday())).replace(hour=0, minute=0, second=0, microsecond=0)
int(start_of_week.timestamp())Formatting for display
Use the language’s locale APIs so users see dates in their format. JavaScript: toLocaleDateString(), toLocaleString(), or options like { dateStyle: 'short' }. Python: strftime or libraries like babel. For “2 days ago” style relative time, many languages have a library (e.g. date-fns/formatDistanceToNow in JS, or implement with the day difference you already have).
We will add a Unix timestamp converter tool in a future update.