Last updated
Common Mistakes to Avoid
- Using Unix epoch (1970) instead of Twitter epoch (Nov 4, 2010) — produces dates 40 years in the past
- Using regular JavaScript numbers instead of BigInt — causes precision loss for large IDs
- Confusing milliseconds and seconds — Twitter timestamps are in milliseconds
- Using the string ID from the URL without converting to integer first
- Forgetting that id_str (not id) should be used in JavaScript API responses
Use the online decoder at the top of this page for instant results without writing any code.
Examples
Example 1: Understanding the Bit Structure
Twitter Snowflake ID: 1382350606417817604
Binary (64 bits):
0 0001001100110000101001001001001001001001001001001001001000000100
Bit layout:
┌─────────────────────────────────────────────────────────────────┐
│ 0 │ 41 bits (timestamp) │ 5 bits (DC) │ 5 bits (worker) │ 12 bits (seq) │
└─────────────────────────────────────────────────────────────────┘
Decoded:
Timestamp bits: → milliseconds since Twitter epoch
Twitter epoch: 1288834974657 (Nov 4, 2010 01:42:54 UTC)
Datacenter ID: 0-31
Worker ID: 0-31
Sequence: 0-4095
Example 2: JavaScript Decoder (using BigInt)
// JavaScript loses precision for integers > 2^53
// Always use BigInt for Twitter Snowflake decoding
const TWITTER_EPOCH = 1288834974657n;
function decodeSnowflake(snowflakeId) {
const id = BigInt(snowflakeId);
// Extract timestamp (bits 22-63)
const timestampMs = Number((id >> 22n) + TWITTER_EPOCH);
// Extract datacenter ID (bits 17-21)
const datacenterId = Number((id & 0x3E0000n) >> 17n);
// Extract worker ID (bits 12-16)
const workerId = Number((id & 0x1F000n) >> 12n);
// Extract sequence number (bits 0-11)
const sequence = Number(id & 0xFFFn);
return {
timestamp: new Date(timestampMs),
timestampMs,
datacenterId,
workerId,
sequence
};
}
// Usage:
const result = decodeSnowflake('1382350606417817604');
console.log(result.timestamp.toISOString());
// → "2021-04-14T17:05:00.123Z"
console.log(result.datacenterId); // → 0-31
console.log(result.workerId); // → 0-31
console.log(result.sequence); // → 0-4095
Example 3: Python Decoder
import datetime
TWITTER_EPOCH = 1288834974657 # milliseconds
def decode_snowflake(snowflake_id: int) -> dict:
"""Decode a Twitter Snowflake ID into its components."""
# Extract timestamp (right-shift by 22 bits)
timestamp_ms = (snowflake_id >> 22) + TWITTER_EPOCH
created_at = datetime.datetime.fromtimestamp(
timestamp_ms / 1000,
tz=datetime.timezone.utc
)
# Extract datacenter ID (bits 17-21)
datacenter_id = (snowflake_id & 0x3E0000) >> 17
# Extract worker ID (bits 12-16)
worker_id = (snowflake_id & 0x1F000) >> 12
# Extract sequence number (bits 0-11)
sequence = snowflake_id & 0xFFF
return {
'created_at': created_at,
'timestamp_ms': timestamp_ms,
'datacenter_id': datacenter_id,
'worker_id': worker_id,
'sequence': sequence
}
# Usage:
result = decode_snowflake(1382350606417817604)
print(result['created_at']) # 2021-04-14 17:05:00+00:00
print(result['datacenter_id']) # 0-31
print(result['worker_id']) # 0-31
print(result['sequence']) # 0-4095